Re: Generic decorators and type hinting

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> Is there another way to cleanly wrap method calls for timing/logging
> purposes?

I have a possibly-evil idea that gets around type-hinting by
dynamically declaring decorator classes as children of the real
classes that need to be timed. You end up with as many "decorators" as
you have classes that need to be timed, but if this is for dev/QA
purposes only, that might not be a problem.

This method can't have the conceptual simplicity of yours,
unfortunately, because __call() will never be invoked in a child
class. The original parent's methods will just be called instead. But
you can still override the parent's methods with wrapper methods in
the child class.

I've got some sample code written that first calls get_class_methods()
to list the names of all the visible methods of the parent object,
then constructs code to declare a child class that overrides those
methods with wrapper methods, and finally eval()s the declaration and
instantiates an object of the new class.

The basic concept seems to work and to get along fine with
type-hinting. I'm happy to share the test code if you're interested.
Not sure how it would end up working in real life, and I'm guessing
there are more sophisticated ways to achieve the same concept, perhaps
using the Reflection API. I suspect it also needs some refining to
handle protected methods. But it might be a start.

Ben

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux