开发者

filepath of children

开发者 https://www.devze.com 2023-02-06 20:52 出处:网络
class a1 extends class b{} class a2 extends class b{} class a3 extends class b{} class a4 extends class b{}

class a1 extends class b{}
class a2 extends class b{}
class a3 extends class b{}
class a4 extends class b{}
class a5 extends class b{}

I need $this->filePath in each a1 to a5 that point to location of it's file, but when I set $this->filePath = __FILE__ in parent, in children $this->f开发者_运维百科ilePath point to parent location


There is no way to do this globally in b's constructor due to the nature of __FILE__ - it does not behave like a function, but is a magic constant that gets processed (and replaced with its actual value) when the file is interpreted.

You will have to do this in each child separately. This works in PHP 5:

class a1 extends b{ private $path = __FILE__;}
class a2 extends b{ private $path = __FILE__;}
class a3 extends b{ private $path = __FILE__;}
class a4 extends b{ private $path = __FILE__;}
class a5 extends b{ private $path = __FILE__;}

The only way I know to do this in the parent is using debug_backtrace(), and that is not a good practice.

0

精彩评论

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