From: Maximiliano Churichi on 21 May 2010 14:46 Is there a way to catch EVERY method of a inheritance class? something like this... class Father { public function __call ($name, $args) { $this->doSomethingBefore(); if ( method_exists($this, $name) ) return $this->$name($args); else echo 'invalid method'; } public function doSomethingBefore () { echo 'something before called method'; } } class Son extends Father { public function insert () { echo 'inserting'; } } $johnDoe = new Son(); $johnDoe->insert(); /* -- Expected return: something before called method inserting */ $johnDoe->foo(); /* -- Expected return: something before called method invalid method */ Greetings... -- Maximiliano Churichi <mchurichi(a)gmail.com>
From: Adam Richardson on 21 May 2010 15:53 On Fri, May 21, 2010 at 2:46 PM, Maximiliano Churichi <mchurichi(a)gmail.com>wrote: > Is there a way to catch EVERY method of a inheritance class? > something like this... > > class Father > { > public function __call ($name, $args) > { > $this->doSomethingBefore(); > > if ( method_exists($this, $name) ) > return $this->$name($args); > else > echo 'invalid method'; > } > > public function doSomethingBefore () > { > echo 'something before called method'; > } > } > > class Son extends Father > { > public function insert () > { > echo 'inserting'; > } > } > > $johnDoe = new Son(); > > $johnDoe->insert(); > /* > -- Expected return: > something before called method > inserting > */ > > $johnDoe->foo(); > /* > -- Expected return: > something before called method > invalid method > */ > > Greetings... > > -- > Maximiliano Churichi > <mchurichi(a)gmail.com> > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > I played with composition as opposed to inheritance to provide hooks. The wrapper class provided the hooks for intercepting calls before and after on a delegate object. So, if I had a class like below: class Greet{ public function greet($message){ echo $message; } } To capture calls, I'd do the following: $obj = new Interceptor(new Greet()); // requires PHP 5.3 $obj->register($interceptMethod = 'greet', $interceptor = function($message){ return strtoupper($message);}, $when = Interceptor::BEFORE); $obj->greet("Hello"); // outputs "HELLO" Sorry, I didn't use inheritance in any of my work. That said, there are some php aspect oriented projects out there and perhaps their code could provide some clues as to how to get what you're looking for: http://code.google.com/p/php-aop/ http://www.aophp.net/ Adam -- Nephtali: PHP web framework that functions beautifully http://nephtaliproject.com
|
Pages: 1 Prev: Help, FPDI is changing the size of my PDFs Next: localize string (date) |