我已经阅读了很多类似的问题和答案,但似乎没有一个能解决我的情况。
A 级
class A{
public function walk() {
...
}
public function dance() {
require 'dance.php';
}
}
现在在 dance.php
中,我有
$this->walk();
我得到这个错误: fatal error :不在对象上下文中时使用 $this
请帮忙。是我不能在所需文件中使用 $this 吗?
谢谢
最佳答案
我已经在干净的环境中对其进行了测试,它对我来说运行良好。
我假设您可能在代码中不在对象上下文中的其他地方包含了 dance.php。
另请查看Possible to access $this from include()'d file in PHP class?这可能是一个重复的问题。
关于php - 使用 require 时为 "Fatal error: Using $this when not in object context"- PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39993118/