Regarding paths:

Topics: Project Management Forum, User Forum
Jul 2, 2009 at 5:11 PM


I understand that the code paths are reliant on the PHPExcel directory being within the include_path environment variable, where we can't simply link to "includes/PHPExcel" or "classes/PHPExcel".

If I may ask, why are the paths not relative to each other?  A user would have to muck about to include the path to PHPExcel in the include_path (either hardcoding within php.ini, or using ini_set())

For example: in Writer/PDF.php, there are includes to "PHPExcel/Writer/IWriter.php" and "PHPExcel/Cell.php".  Typically, I would expect to see these be relative paths: "./IWriter.php" and "../Cell.php".


If this is a matter of "That's the way it is and nobody wants to go through and change it", I'll gladly take the 20 minutes to change it for the project.  ?





Jul 2, 2009 at 5:40 PM
Edited Jul 2, 2009 at 5:41 PM

The problem is that in PHP the included file is taken relative to the executed script, not relative to the file where the include statement occurs. If A includes B and B includes C, then your method will not work in general if A and B are in different directories. This is a big pain in PHP.

One can get around this problem by using absolute path together with magical constant __FILE__

require_once dirname(__FILE__) . '/IWriter.php';

This is perhaps an option, and one would not have to set up an include path. Last time I looked at this, I came across posts saying that this was bad practice presumably because of extra function calls with dirname(__FILE__). But how much will this be slowing down the script?

Maybe we should discuss this option. I agree that it is annoying to deal with include paths. Maybe Maarten or Mark has an opinion about this.

Jul 2, 2009 at 6:20 PM
Edited Jul 2, 2009 at 6:21 PM

I just ran a quick test, with the following directory structure:

Each has essentially the same format:
  print "We're currently at " . __FILE__ . "<BR>";

You can guess how this goes - inc calls "include/1.php", which calls "2.php", which calls "first/11.php", which calls "12.php", no problems.  The stack breaks when I hit "../second/21.php" - I have some module which handled this kind of thing on the server (A coworker implemented this, not sure the module / code that does it, definitely a "nice to have" that I never even think about).

One other option which I've seen in a variety of 3rd party scripts would be to have a constant defined with root path configured.  Within PHPExcel.php, you could have the following simple line:

  define("PHPEXCELPATH", dirname(__FILE__) . "/PHPExcel/");

Using this idea, you only run the function of dirname(__FILE__) once, store the result in a constant, have a fairly accessible system.. but this is reliant on calling PHPExcel.php to initiate the PHPExcel scripts.

Jul 2, 2009 at 6:47 PM

Ok, I have posted this as a feature request:

Let's see what inputs we get for this.