加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

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

发布时间:2020-05-25 09:10:31 所属栏目:PHP 来源:互联网
导读:这是一个例子: class Test { public function TestMethod() { print_r($this); // Gives me Test1 Object ( ) }}class Test1 { public function Test1Method() { Test::TestMethod(

这是一个例子:

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,它将调用警告.

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读