SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: path issues

  1. #1
    SitePoint Zealot
    Join Date
    Aug 2005
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    path issues

    Hello All

    I am currently reworking a past project and am getting rid of most of the procedural code and replacing it with classes. everything is going quite well but the paths are driving me nuts. I am trying to use one consistent solution for all links and include files but every variation I try seems to have an issue somewhere. I will run down what I have tried and see what your suggestions are.

    1) page relative:
    Code:
    include('../classes/myClass.php');
    this doesn't work when I am more than 1 folder deep. I have a loader class that does not currently account for how many folders deep the call is benig made from.

    2)
    root relative i.e.
    Code:
    include('/myproject/classes/myClass.php');
    This includes properly from where ever it is called, but then I have a problem with instantiating.
    Code:
    include('/myproject/classes/myClass.php');
    myClass = new myClass;
    This gives me an error saying that the myClass class cannot be found. I have looked around but have no clue why this is happening.

    3) absolute paths
    Code:
    include('c:/wamp/www/myProject/classes/myClass.php');
    the includes work fine but my sessions went haywire when I went to this method. My login page does not work anymore and I beleive it is because it is setting 2 different session id's - one for the login page and another when it redirects to the rest of the site.

    Does that make sense?

    Thanks

    Matt

  2. #2
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can set the include_path in php.ini (or an .htaccess file) and then include relative to that.

    Quote Originally Posted by mkdrums View Post
    2)[/B] root relative i.e.
    Code:
    include('/myproject/classes/myClass.php');
    This includes properly from where ever it is called, but then I have a problem with instantiating.
    Code:
    include('/myproject/classes/myClass.php');
    myClass = new myClass;
    This gives me an error saying that the myClass class cannot be found. I have looked around but have no clue why this is happening.
    You aren't including relative in that example. If you prefix with a slash, you're including from the root (Eg. C:\). To include relative, you should start with the dirname. The reason, you're getting an error, is because include doesn't fail, if the file isn't found. You should rarely use include or include_once -- Use require and require_once instead.

  3. #3
    SitePoint Member
    Join Date
    Jul 2006
    Location
    Prague, Czech republic
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My favourite solution, script relative:
    PHP Code:
    require dirname(__FILE__) . "/path/relative/to/the/script/where/it/is/called/from.php"
    E.g. in the script /foo/bar/script.php:
    PHP Code:
    // include /foo/something.php
    require dirname(__FILE__) . "/../something.php"

  4. #4
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    4,998
    Mentioned
    100 Post(s)
    Tagged
    0 Thread(s)
    my preferred way is:

    <?php include $_SERVER['DOCUMENT_ROOT'] . '/path/to/file/thefile.php'; ?>

  5. #5
    SitePoint Zealot
    Join Date
    Aug 2005
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys, 2 of the three ways I was not familiar with and 1 of the three ways I tried didn't work. I will give it another pass.

    As an aside to kyberfabrikken, I was using require_once initially and I was reading in some php book written by some zend guy that include is more efficient than require as it is only "included" when it is needed compared to require which all the requires are loaded regardless of if they are needed or not.

    Just thought I would throw that out there.

    Thanks again;.

    Matt

  6. #6
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mkdrums View Post
    As an aside to kyberfabrikken, I was using require_once initially and I was reading in some php book written by some zend guy that include is more efficient than require as it is only "included" when it is needed compared to require which all the requires are loaded regardless of if they are needed or not.
    Premature optimisation is the root of all evil. Well not all perhaps, but close enough.
    require_once has the functionality you need -- include doesn't. That should be sufficient reason to pick the first. In regard to performance, include and require are identical. To quote the manual:
    The documentation below also applies to require(). The two constructs are identical in every way except how they handle failure. They both produce a Warning, but require() results in a Fatal Error.

  7. #7
    SitePoint Zealot
    Join Date
    Aug 2005
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know, I read the manual - but the zend guy in the book said.... oh well.

    I like the root of all evil quote.

    Got everything working again. I had multiple issues - I went the $_SERVER['DOCUMENT_ROOT'] route since that was the quickest for me to get going.

    I was using a relative path in my session login redirect so that is why that wasn't working.

    Thanks again.

    Matt


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •