开发者

PHP __FILE__ inheritence

开发者 https://www.devze.com 2023-03-26 12:47 出处:网络
I am trying to write a method that I can use in extended classes that uses the file\'s full path.However, when I use __FILE__ I only get back the path for the class the inherited method is defined in.

I am trying to write a method that I can use in extended classes that uses the file's full path. However, when I use __FILE__ I only get back the path for the class the inherited method is defined in. Here is an example:

foo.php

class foo 
{

   开发者_开发知识库 public function getFullFile()
    {
        return __FILE__;
    }
}

bar.php

class bar extends foo 
{
    public function getFile()
    {
        return __FILE__;
    }

}

example.php

$foo = new foo();
echo $foo->getFullFile() . PHP_EOL;

$bar = new bar();
echo $bar->getFullFile() . PHP_EOL;
echo $bar->getFile() . PHP_EOL;

The output of running example.php:

/my/full/path/foo.php
/my/full/path/foo.php
/my/full/path/bar.php

Is this expected behavior? Is there a better way to accomplish this? Am I doing something insanely stupid?


You can not refer to __FILE__ (for said reasons), but you can refer to the file the current object's class has been defined in:

class foo 
{
    public function getFullFile()
    {
        $c = new ReflectionClass($this);
        return $c->getFileName();
    }
}


__FILE__ is always the file where the constant is referred to. It's for debugging purposes to you can easily refer back to the exact file where the code that uses __FILE__ is located. It would be useless to say that it was in 'bar.php', when the definition resides in 'foo.php'.


Some code says more than thousand words...

B.php

<?
class B {
  public function bar() {
    return __FILE__;
  }
}

A.php

<?
require "B.php";
class A extends B {
  public function foo() {
    echo parent::bar();
  }
}
$a = new A();
$a->foo();

joe@joe-desktop:~$ php A.php

/home/joe/B.php


See http://php.net/manual/en/language.constants.predefined.php for more information on these 'magical constants'. If I am not mistaken these magical constants are a sort of macros and are determined at compile time, not runtime.


This is the expected behaviour __FILE__ will always evaluate to the filename of file in which interpreter finds it. Same with other magic constants (__METHOD__, __CLASS__, __LINE__ etc) As far as I can tell, it is impossible to do waht you're trying to do.


I think if you want $bar->getFullFile() to return bar.php, you'll have to add the function to bar.php.

public function getFullFile() {
  return parent::getFullFile();
}


Just use debug_backtrace() PHP function. Example of use (my library function my_die is placed in some included php file, but reports the callers location!):

function my_die($msg)
{
    $bt = debug_backtrace();

    $file = $bt[0]["file"];
    $line = $bt[0]["line"];
    $func = $bt[1]["function"];

    # session info also!!!
    $user = $_SESSION['user'];

    error_log('Died in ' . $file . ': ' . $line . ': function ' . $func .
            "(): user='{$user}': ip='{$_SERVER['REMOTE_ADDR']}': " . $msg .
            " (browser: '{$_SERVER['HTTP_USER_AGENT']})'");
    die($msg);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消