php - Output a property with PHP5 and method chaining -


i playing php5 , method chaining, following several stackoverflow examples. set generic show() method able print desired property, please see example:

<?php  class testarea{    public function set_a(){     $this->property_a = 'this a'.php_eol;     return $this;   }    public function set_b(){     $this->property_b = 'this b'.php_eol;     return $this;   }    public function show(){    echo var_dump($this->property_a); // ->... generalize                                                                                                                         return $this;   }  }  $ta=new testarea();  $ta->set_a()->set_b(); $ta->show();  ?> 

this echoes string(10) "this ".

what generic show() method shows property set_a() or set_b() methods have setted.

is possible?

create private array property:

private $last = null; private $setlist = array(); 

in set_a() , set_b() use:

$this->last = 'line a'; $this->setlist['a'] = $this->last; 

and

$this->last = 'line b'; $this->setlist['b'] = $this->last; 

your show() method reads:

foreach ($this->setlist $line) {   var_dump($line); } 

or if need last property set:

return $this->last; 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -