I have a helper function helper.php
with contents:
<?php
session_start();
function get_result($dbh, $sql) {
//return mssql_query($sql);
return $dbh->query($sql);
}
?>
which is included in two files (information.php
and commercial.php
) using:
include_once 'helper.php'
Unfortunately this generates the slightly confusing error message:
PHP Fatal error: Cannot redeclare get_result() (previously declared in helper.php:4) in helper.php on line 4
I understand that i cannot redeclare functions, hence why i use the include_once
construct, but nevertheless it tries to redeclare the function anyway. Why?
If it helps; I am using Mustache PHP, and all three files are located in the partials
folder.
include_once
ensures that file is included exactly once. It doesn't check the contents of the file or the functions in it. So when two file with same function name is added, its quite natural for the error to arise!
From the manual:
include_once
may be used in cases where the same file might be included and evaluated more than once during a particular execution of a script, so in this case it may help avoid problems such as function redefinitions, variable value reassignments, etc.
italicized means that the function in the same file, not in different files.