加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP __FILE__继承

发布时间:2020-12-13 22:08:51 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试编写一个方法,我可以在使用该文件的完整路径的扩展类中使用.但是,当我使用__FILE__时,我只返回定义继承方法的类的路径.这是一个例子: foo.php class foo { public function getFullFile() { return __FILE__; }} bar.php class bar extends foo {
我正在尝试编写一个方法,我可以在使用该文件的完整路径的扩展类中使用.但是,当我使用__FILE__时,我只返回定义继承方法的类的路径.这是一个例子:

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;

运行example.php的输出:

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

这是预期的行为吗?有没有更好的方法来实现这一目标?我做了一些疯狂的蠢事吗?

解决方法

你不能引用__FILE__(对于 said reasons),但你可以参考文件中定义了当前对象的类:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读