孩子的文件路径
<代码>
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{}
我需要 $this->filePath 在每个 a1 到 a5 中指向其文件的位置,但是当我在父级中设置 $this->filePath = __FILE__ 时,在子级中 $this->filePath 指向父级位置
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->filePath point to parent location
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于 __FILE__ 的性质,无法在 b 的构造函数中全局执行此操作 - 它的行为不像函数,而是一个需要处理的魔术常量(并替换为其实际值)当解释文件时。
您必须分别为每个孩子执行此操作。这在 PHP 5 中有效:
我知道在父级中执行此操作的唯一方法是使用 debug_backtrace(),但这不是一个好的做法。
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:
The only way I know to do this in the parent is using
debug_backtrace()
, and that is not a good practice.