Errors Related to Same Path Through Real and Symbolic Link References [message #178710] |
Sun, 22 July 2012 03:53 |
SMH
Messages: 3 Registered: July 2012
Karma: 0
|
Junior Member |
|
|
I am using "require_once" to include a PHP file that defines constants and
commonly used functions. It is in a main script and also in support scripts
that are themselves included in the main script. The main script is in a
"development" directory, and that directory contains a symbolic link to
included files. I am getting a FATAL ERROR (function in included file is
re-declared) because there is a reference to an included script through a
real path and one through a symbolically linked path.
Here is more detail:
The included file is named "globalsLib.php." In it are defined constants
and common functions. Important in this post is that there is a function
__autoload() defined on line 39.
This file "globalsLib.php" is located in the directory
"Z:\service.web.Root\PHPClasses" (yes, the system is Windows [7])
I also have a directory "Z:\Development". It contains a subdirectory
"PHPClasses" which is symbolically linked to "Z:\service.web.Root
\PHPClasses". and so the "Z:\Development\PHPClasses" points to that as the
real directory.
Inside "Z:\Development" is a script "index.php" which contains as follows:
line 5: require_once('globalsLib.php');
line 19: include 'SySInfo.php';
Inside the "SysInfo.php" file on line 11 is require_once('globalsLib.php');
Here is the PHP output when requesting "http://localhost/development/"
(which reads the script "Z:\Development\index.php"
Notice: Constant PATH_SEPARATOR already
defined in Z:\service.web.Root\PHPClasses\globalsLib.php
on line 26
Fatal error: Cannot redeclare __autoload() (previously
declared in Z:\service.web.Root\PHPClasses\globalsLib.php:39)
in Z:\Development\PHPClasses\globalsLib.php on line 41
All of this is clearly caused by PHP correctly not knowing what that a
symbolic link is pointing to the same directory.
I am almost certainly violating at least ONE, and probably a DOZEN Best
Practice rules in using PHP. Either you know the name of a book or two on
PHP or general programming that I absolutely MUST read, or you have a set of
rules you have developed yourself.
I would appreciate your advice.
|
|
|
Re: Errors Related to Same Path Through Real and Symbolic Link References [message #178711 is a reply to message #178710] |
Sun, 22 July 2012 06:30 |
J.O. Aho
Messages: 194 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 22/07/12 05:53, SMH wrote:
> I am using "require_once" to include a PHP file that defines constants and
> commonly used functions. It is in a main script and also in support scripts
> that are themselves included in the main script. The main script is in a
> "development" directory, and that directory contains a symbolic link to
> included files. I am getting a FATAL ERROR (function in included file is
> re-declared) because there is a reference to an included script through a
> real path and one through a symbolically linked path.
The obvious solution is to see that you include the file just once.
Another work around is to use c/c++ like define checks:
<?php
if(!defined('__MYPHPFILENAME'))
{
// define the value, so next include won't come in here
define('__MYPHPFILENAME', true);
function first_function_of_yours() {
// your code here
}
function second_function_of_yours() {
// your code here
}
}
?>
or you could do for each function:
if(!function_exists('first_function_of_yours')) {
function first_function_of_yours() {
// your code here
}
}
and you have to see if values are defined before you define those (if
you use global values).
If you really want to do it the right way, just see to just include the
file once, no matter if it's the original file or a symlink.
--
//Aho
|
|
|
|