Class Test {Public $a, $b;p ublic function __construct ($a, $b) {$this->a= $a; $this->b= $b;} Public function set ($var, $val) {$this-$var. = $val; return $this;} Public function Get () {echo $this->a. '-'. $this->b;}} Class Test2 {public $obj;p ublic function __construct ($obj) {$this->obj= $obj;} Public function Get () {$this->obj->get ();}} $a = ' aaaaa '; $b = ' bbbbbb '; $set = ' set ';///1, chain call method $test = new test ($a, $b); $test 2 = new Test2 ($test); $test--$set (' a ' , ' 111111 ') $set (' B ', ' 222222 '); Call (Class) method through a string variable ps:php another ingenious application///2, sub-call Method $test-> $set (' A ', ' 111111 '), $test-$set (' B ', ' 222222 '); unset ($test); $test 2->get ();
Although the Test2 instantiation has passed in test, test can still set the parameters through the set method and then affect Test2.
Results of the above execution procedure: (both of the above can produce the same result)
aaaaa111111-bbbbbb222222
PHP Object-oriented Smart application