[Phpwm] PHP eclipse problem

Ian Munday ian.munday at illumen.co.uk
Wed Apr 26 12:43:22 BST 2006


My projects are structured along the following lines:

/project/config/		# configuration file(s)
/project/includes/		# 3rd party include files
/project/library/		# my own library (class) files
/project/scripts/		# scripts (pages) of the website/application

In each PHP page (both scripts and library files) I declare a  
ROOT_PATH variable page and then refer to this:

<?php

if (!defined('ROOT_PATH'))
{
	define('ROOT_PATH', '../');
}

require_once(ROOT_PATH . 'config/config.php');
require_once(ROOT_PATH . 'config/session.php');
require_once(ROOT_PATH . 'library/MyObject.class.php');
....

?>

This solves the nested include problem when I've come across it and,  
whilst this certainly works for me, I'm also open to the pros / cons  
of this way of doing things.

An obvious con is where a project is restructured given that I  
explicitly write the path of the project root to each file.  But  
given that I'm consistent with this way of doing things, the solution  
is invariably no more than a simple find-and-replace.

Ian


On 26 Apr 2006, at 09:54, Elliot Smith wrote:

> Pete,
>
> A few comments:
>
> 1. Instead of having two separate, almost identical sets of code  
> for setting up the include path, it might be better to use the  
> DIRECTORY SEPARATOR and PATH_SEPARATOR magic PHP constants, like this:
>
> $_PATHS["includes"]      = $_PATHS["base"] . "includes" .  
> DIRECTORY_SEPARATOR;
> $_PATHS["templates"]    = $_PATHS["base"] . "templates" .  
> DIRECTORY_SEPARATOR;
>
> ...
>
> ini_set("include_path", "." . PATH_SEPARATOR . "$_PATHS 
> [includes]" . PATH_SEPARATOR . "$_PATHS[templates]" ...
>
> ...
>
> (truncated for brevity). That way you can set the paths for any OS  
> with the same code.
>
> 2. I think it's a matter of taste whether you prefer:
>
> include_once("../includes/functions.php");
>
> OR
>
> include_once("functions.php");
>
> Personally, I prefer the former, as it is more explicit about where  
> the code you're including resides. It's not so obvious in the  
> latter, and I'd prefer clarity over brevity any day. Plus there's  
> less chance of namespace clashes (e.g. if there is a functions.php  
> file in more than one place on the include path, PHP might include  
> the wrong one).
>
> My personal tendency would be to create a single init style script,  
> which pulls in all the libraries I'm going to need, then just  
> include the init script where I need the libraries. I know this may  
> pull in more code than I actually need for some scripts; but I  
> think the extra time spent doing this is probably little more than  
> the extra time PHP would spend resolving paths where they are not  
> explicit (as is the case if you append lots of extra paths to the  
> include_path ini variable). Interesting one, though.
>
> 3. Filtering problem messages in Eclipse: get the Problems view up;  
> click the little button with arrows on it (top right); in the pop- 
> up window, at the bottom, there's an option to filter problems  
> shown, based on text within the problem (exclude or include).
>
> Elliot
>
>
>
>
>
> pete graham wrote:
>
>> Elliot,
>>
>> Here is a better explanation of how I structure my code. In the  
>> eclipse project I wanted to set projectname as the root directory.
>>
>> Now projectname/htdocs is the webroot. Every single file in htdocs  
>> includes a file called config.inc.php. The file includes this code:
>>
>> if(preg_match("/WINDOWS/i", $_SERVER["SystemRoot"]))
>> {
>>     // THIS IS A WINDOWS MACHINE
>>
>>     ## Include paths
>>     $_PATHS["base"]            = dirname(dirname(__FILE__)) . "\\";
>>     $_PATHS["includes"]        = $_PATHS["base"] . "includes\\";
>>     $_PATHS["templates"]    = $_PATHS["base"] . "templates\\";
>>     $_PATHS["pear"]            = $_PATHS["base"] . "pear\\";
>>     $_PATHS["logs"]            = $_PATHS["base"] . "logs\\";
>>     $_PATHS["sidebar"]        = $_PATHS["base"] . "sidebar\\";
>>
>>     /**
>>     * Set include path
>>     */
>>     ini_set("include_path",
>>             ".;$_PATHS[includes];$_PATHS[templates];$_PATHS[pear]; 
>> $_PATHS[sidebar]");
>>
>> }else{
>>     // NOT A WINDOWS MACHINE (Linux, Mac, FreeBSD)
>>
>>     ## Include paths
>>     $_PATHS["base"]            = dirname(dirname(__FILE__)) . "/";
>>     $_PATHS["includes"]        = $_PATHS["base"] . "includes/";
>>     $_PATHS["templates"]    = $_PATHS["base"] . "templates/";
>>     $_PATHS["pear"]            = $_PATHS["base"] . "pear/";
>>     $_PATHS["logs"]            = $_PATHS["base"] . "logs/";
>>     $_PATHS["sidebar"]        = $_PATHS["base"] . "sidebar/";
>>
>>     /**
>>     * Set include path
>>     */
>>     ini_set("include_path",
>>             ".:$_PATHS[includes]:$_PATHS[templates]:$_PATHS[pear]: 
>> $_PATHS[logs]:$_PATHS[sidebar]");
>> }
>>
>> this lovely bit of code means that I don't have to type:
>>
>> include_once("../includes/functions.php");
>>
>> I can just do this instead:
>>
>> include_once("functions.php");
>>
>> Unfortunately eclipse does not seem to enjoy this style of  
>> structuring which is a shame because I find it highly effective.
>>
>> I shall have to look into your suggestion of using a problems  
>> filter, that sounds promising.
>>
>> Pete
>>
>>
>>
>> On 4/26/06, *Elliot Smith* < elliot at townx.org  
>> <mailto:elliot at townx.org>> wrote:
>>
>>     Dear Pete,
>>
>>     Although I don't organise my code like this, from what I know of
>>     PHPEclipse, it should resolve paths correctly if you did your  
>> includes
>>     something like this (presuming you set projectname as the root
>>     directory
>>     when creating your Eclipse project):
>>
>>     require_once('../includes/file.php');
>>
>>     I assume you're doing something like this, rather than using  
>> absolute
>>     paths? Another approach may be to just exclude those messages  
>> from the
>>     problems list using a filter.
>>
>>     Elliot
>>
>>
>>     pete graham wrote:
>>
>>     > I have been experimenting with PHPeclipse yesterday and today.
>>     Here is
>>     > a fairly big problem that I have run into that has stopped  
>> me from
>>     > using it.
>>     >
>>     > Eclipse generates a lot of warnings of type: "Include filename
>>     > 'blah.php' doesn't exist in project."
>>     >
>>     > I tried to explicitly set the "Include Path" for the project in
>>     > "Project->Properties->PHP Project Settings->Include Paths"
>>     (although
>>     > this shouldn't be necessary because these paths are dynamically
>>     set by
>>     > a set_include_path() call in the script). This hasn't solved  
>> the
>>     problem.
>>     >
>>     > All my projects have this structure
>>     >
>>     > projectname/htdocs/        # actual website root
>>     > projectname/includes/      # mostly php classes and files
>>     > with programming logic
>>     > projectname/templates/   # html files, headers footers, etc
>>     >
>>     > I find this an effective way of organising the files. It  
>> seems the
>>     > only way to get PHPeclipse to not generate the errors is to
>>     place all
>>     > code in the website root which I really don't want to do. Can
>>     anyone
>>     > help me on this problem?
>>     >
>>     > I saw on a forum post that "working sets" may be able to  
>> solve my
>>     > problem, but I'm quite unclear on what these are.
>>     >
>>     > Pete
>>     >
>>      
>> >-------------------------------------------------------------------- 
>> ----
>>
>>     >
>>     >_______________________________________________
>>     >Phpwm mailing list
>>     >Phpwm at mailman.lug.org.uk <mailto:Phpwm at mailman.lug.org.uk>
>>     > https://mailman.lug.org.uk/mailman/listinfo/phpwm
>>     >
>>     >
>>
>>
>>     _______________________________________________
>>     Phpwm mailing list
>>     Phpwm at mailman.lug.org.uk <mailto:Phpwm at mailman.lug.org.uk>
>>     https://mailman.lug.org.uk/mailman/listinfo/phpwm
>>
>>
>> --------------------------------------------------------------------- 
>> ---
>>
>> _______________________________________________
>> Phpwm mailing list
>> Phpwm at mailman.lug.org.uk
>> https://mailman.lug.org.uk/mailman/listinfo/phpwm
>>
>
>
> _______________________________________________
> Phpwm mailing list
> Phpwm at mailman.lug.org.uk
> https://mailman.lug.org.uk/mailman/listinfo/phpwm




More information about the Phpwm mailing list