__DIR__ VS using Reflection

In Symfony2, I saw the code as shown below:

    if (null === $this->rootDir) {
        $r = new \ReflectionObject($this);
        $this->rootDir = dirname($r->getFileName());
    }

why not just use it __DIR__?

    if (null === $this->rootDir) {
        $this->rootDir = __DIR__;
    }

What is the difference between the two?

+3
source share
4 answers

__DIR__returns the directory of the file in which it is called. Symphony2 code returns the directory where the class is defined, which is most likely another file.

+5
source

As stated in the PHP manual:

  • DIR returns the directory of the file. If used inside include, returns the directory with the included file
  • FILE returns the full path and file name. If used inside include, the name of the included file is returned.

, , . , , . , - , . , .

+2

__DIR__exists only in PHP 5.3. Before 5.3, we had to use dirname(__FILE__)or something like that.

0
source

I think this is because it __DIR__will return the directory from the script that was originally called. In the code example, you will get the object's class directory. Maybe I'm wrong, although I have not tried it yet, but please correct me if I will.

0
source

All Articles