Welcome Guest, Not a member yet? Register   Sign In
Question about how PHP resolve current directory (".")
#1

[eluser]ang89[/eluser]
Hi there!

My question is straightforward, but I do have something to explain. Please jump to the last paragraph if you don't want to bother my whole coding epics.

In CodeIgniter, when I try to resolve current directory by using realpath("."), I always got the directory in which the gateway index.php is located (in my case, /webroot). Regardles the location of current file (model, controller, config, library), it's all the same.

But I came to a problem where I have to create another controller from within a library (I can explain why if you would like to know, but I think it's not important here). And from within the controller class, I have the following:

Code:
<?php

class Test_service extends Controller
{
    public function __construct()
    {
        $CI =& get_instance();
        $CI->firephp->log('The path inside Test_service controller: ' . realpath('.'));
        parent::__construct();
    }
}

Surprisingly, it outputs the location in which the file is located (/application/controllers/services), not the /webroot !.
And the parent::__construct off course gives an error because ... well, if you follow the trace you can find an include_once(APPPATH.'config/autoload'.EXT) inside Loader class.

After a long and maybe irrelevant explaination, and my question is, how does PHP resolve current directory (".")? I thought it should be the directory of the first file in the chain of execution (index.php) ?
#2

[eluser]danmontgomery[/eluser]
parent::__construct(); doesn't exist, you need to call parent::Controller();

realpath() returns the working directory, which is normally the location of index.php. If that's not the case, the working directory is being changed at some point: http://php.net/manual/en/function.chdir.php
#3

[eluser]ang89[/eluser]
Quote:parent::__construct(); doesn't exist, you need to call parent::Controller();

I think if it is declared as parent::Controller(), calling parent::__construct() will works fine (and no difference). Reference.

Quote:realpath() returns the working directory, which is normally the location of index.php. If that's not the case, the working directory is being changed at some point: http://php.net/manual/en/function.chdir.php

Ah yes, getcwd() also tells that working directory have been changed. It's the issue with the library I'm using then. Thanks !




Theme © iAndrew 2016 - Forum software by © MyBB