Wednesday, 15 July 2015

php chain method return variable and method calls another method -



php chain method return variable and method calls another method -

i saw different articles chain method, still don't understand difference between "return $this" , "return $this->somevariable".i want know how method phone call method within , without class too.

could kindly explain ? you!

my example, echo "bca", dont why "a" lastly display...

class validation { public function __construct($a) { $this->a = $a; } public function one($a) { echo $a = "b"; homecoming $this; } public function two($a) { echo $a = "c"; homecoming $this->a; } } $a = "a"; $nameerr = new validation($a); echo $nameerr->one($a)->two($a);

it returned two($a) since returns $this->a set in constructor "a", , method one($a) returns instance of object on called function two.

$this refers object instance. difference homecoming $this->somevariable returns variable.

also nice coding tip. declare $a in class private variable this:

class validation { private $a; }

php return-value method-chaining

No comments:

Post a Comment