Re: PEAR_Autoloader
| From: | Bertrand Mansion | Date: | Tue, 28 Oct 2003 23:13:02 +0000 |
| Subject: | Re: PEAR_Autoloader | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-23100@lists.php.net to get a copy of this message | ||
<LingWitt@insightbb.com> wrote :
> I've been using PEAR_Autoloader to create what is known in ObjC as
> delegation (Its not actually a language feature though). The idea is
> that an object (or multiple objects) can be made the delegate of
> another object. When this relationship is established, the delegate
> gets a reference to the "owning" object so that it can perform certain
> tasks on the object (if required). Thus, with the __call method, it is
> like adding already implemented interfaces to an object. This feature
> is pretty rudimentary, but powerful, and it seems like it would be an
> interesting addition to the PEAR package. The setup could be as follows:
>
> interface PEAR_DelegateInterface
> {
> public function setOwner($owner);
> public function getOwner();
> }
>
> class PEAR_DelegateOwner extends PEAR_Autoloader
> {
> public function addDelegate($classname)
> {
> $this->addAggregateObject($classname);
> }
>
> public function addAggregateObject($classname)
> {
> $classname = strtolower($classname);
> if (!class_exists($classname)) {
> $include_file = preg_replace('/[^a-z0-9]/i', '_',
> $classname);
> include_once $include_file;
> }
> $obj = new $classname;
> if ($obj instanceof PEAR_DelegateInterface)
> {
> $obj->setOwner($this);
> }
> else
> {
> return;
> }
> $methods = get_class_methods($classname);
> foreach ($methods as $method) {
> // don't import priviate methods and constructors
> if ($method{0} != '_' && $method != $classname) {
> $this->_method_map[$method] = $obj;
> }
> }
> }
> }
>
> As an example:
>
> class Delegate implements PEAR_DelegateInterface
> {
> var $_owner;
>
> public function setOwner($owner)
> {
> $this->_owner = $owner;
> }
>
> public function getOwner()
> {
> return $this->_owner;
> }
>
> public function hello()
> {
> $this->_owner->_message = "Hello!";
> }
> }
>
> class Foo extends PEAR_DelegateOwner
> {
> var $_message = "hi";
> }
>
> $foo = new FOO();
> $foo->addDelegate("Delegate");
> $foo->hello();
>
> $foo->_message now says hello.
An object can only have one delegate, so your method should be
setDelegate(), not addDelegate(). Also, I am not sure it's a good idea to
set a owner in the delegate, after all, the delegate shouldn't care who's
its owner. Then hello() could be hello($owner), but I think that having the
delegate modify its owner is probably not a good thing, just a feeling I
have. I see a delegate as a slave. The slave can't change the master.
I am also not sure an owner needs to know about all the methods in its
delegate, I think it should just test if a method_exists() in the delegate
before calling that method. An owner could probably have a special instance
var with the delegate methods it is likely to call.
Delegates would be a really cool addition to PHP.
Bertrand Mansion
Mamasam