PHP:一个类中的 $this 变量如何成为另一个类的对象?

标签 php oop this static-methods

这是一个例子:

class Test {

    public function TestMethod() {

        print_r($this); // Gives me "Test1 Object ( )"

    }
}

class Test1 {

    public function Test1Method() {

        Test::TestMethod();

    }
}

$test1 = new Test1;
$test1->Test1Method();

我觉得这很奇怪。任何人都可以向我解释为什么会这样吗?

最佳答案

来自 http://www.php.net/manual/en/language.oop5.basic.php :

The pseudo-variable $this is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs, but possibly another object, if the method is called statically from the context of a secondary object).

不过,这不一定很有意义,如果启用了 E_STRICT,则会引发警告。

关于PHP:一个类中的 $this 变量如何成为另一个类的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4760370/

相关文章:

php - 我将如何设置横幅或如何从我的页面中删除此空间

Javascript:从类内部创建全局作用域函数

ios - 为什么 "[self class] == [super class]"?

在 jquery 回调中调用时出现 TypeScript "this"范围问题

jquery - jQuery中的$(this)不是循环变量?

php - Youtube Api率(不喜欢)发布请求不起作用

php - 表单上的空白字段将考虑预先存在的值

javascript - 如何在 JavaScript 中无需任何内联函数调用即可获取点击时的元素 id?

php - 如何在 Laravel 中解码 JSON 对象

asp.net - 对于 HiddenField,Visible 属性有什么用?