Errors Related to Same Path Through Real and Symbolic Link References

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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

On 22/07/12 05:53, SMH wrote:
Quoted text here. Click to load it

The obvious solution is to see that you include the file just once.

Another work around is to use c/c++ like define checks:

    // 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.



Re: Errors Related to Same Path Through Real and Symbolic Link References

SMH wrote:
Quoted text here. Click to load it

I use the on-line manual at: /

in combination with the paper book "PHP: The
Complete Reference" by Steven Holzner.

Martin Leese
E-mail: please@see.Web.for.e-mail.INVALID
Web: /

Site Timeline