简体   繁体   中英

PHP Method Chaining With Parent

Lets say i have the following code.

class A {
    function one() {
        return $this;
    }
}

class B extends A {
    function two() {
        return $this;
    }
}

Is there any way possible that i can method chain using a function from the parent class? Such as..

$b = new B();
$b->one()->two();

Refer to this question for several responses that explain the meaning of the special variable $this .

In short, it refers to the current object. You're creating an instance of B , so even if you refer to $this within function one() you're still referring to the enclosing instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM