I want to understand what is the difference between these chaining method
$obj->prop_a()->prob_b($y);
and
$obj->prop_a->prob_b($x);
how to define it and how it works.
Thanks in advance
On
$obj->prop_a()->prob_b($y);
you access prop_a() as a function . It return (obviously) an object, which implements an prob_b()-function.
On
$obj->prop_a->prob_b($x);
you access prop_a as a public property which contains an object, which again implements an prob_b() function.
一个调用prop_a
作为方法,另一个不调用。
This:
class A {
public function prop_b($x) {
return 4*$x;
}
}
class B {
public function prop_a() {
$a = new A();
return $a;
}
}
class C {
public $prop_a;
public function __construct() {
$this->prop_a = new A();
}
}
$b = new B();
$c = new C();
$b->prop_a()->prob_b(5);
$c->prop_a->prop_b(5);
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.