php - 使用 require 时为 "Fatal error: Using $this when not in object context"- PHP

标签 php this

我已经阅读了很多类似的问题和答案,但似乎没有一个能解决我的情况。

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/

相关文章:

javascript - Cckeditor 从数据库加载模板

javascript - 保持复选框被选中,刷新并提交后

php - 我无法将值传递给 uploadify scriptData

php - 使用php上传图片到sql数据库时显示错误

javascript - 如何使用传递给乐趣的对象作为内部函数的上下文?

jQuery 如果 "this"包含

C++:返回重载 [] 和 "this"

php - "public/index.php"在 laravel 中可以按组写入

javascript - 在 JavaScript 中调用函数时自定义

javascript - 我可以向当前 "this"对象添加属性吗?如何?