class - How to call method before all public methods in PHP? -


i need method run before each public method.

is there method __call public methods?

i want trim arguments before setter methods.

no, there no mechanism __call public methods. __call() looking for.

i define "pseudo public" interface using __call:

class {      protected $value;      /**      * enables caller call defined set of protected setters.      * in case "setvalue".      */     public function __call($name, $args) {         // simplified code, brevity          if($name === "setvalue") {             $propertyname = str_replace("set", "", $name);         }          // desired method should called before setter         $value = $this->dosomethingwith($propertyname, $args[0]);          // call *real* setter, protected         $this->{"set$propertyname"}($value);     }      /**      * *real*, protected setter      */     protected function setvalue($val) {         // validate($val); ? ;)         $this->value = $val;     }      /**      * method should called      */     protected function dosomethingwith($name, $value) {          echo "attempting set " . lcfirst($name) . " $value";          return trim($value);     } } 

if try example:

$a = new a(); $a->setvalue("foo"); 

... you'll following output:

attempting set value foo 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -