Re: PEAR_Autoloader
| From: | LingWitt at insightbb dot com | Date: | Tue, 28 Oct 2003 22:19:57 +0000 |
| Subject: | Re: PEAR_Autoloader | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-23096@lists.php.net to get a copy of this message | ||
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.
On Tuesday, Oct 28, 2003, at 16:42 America/New_York, Jesus M. Castagnetto wrote:
The aggregate functions are no more (in PHP 5): http://marc.theaimsgroup.com/?l=phpdoc&m=105678544210196&w=2 See also the thread that starts here: http://marc.theaimsgroup.com/?l=php-dev&m=105659237905014&w=2 Basically the runtime aggregation is superseded by interfaces. Even though I liked the idea (and wrote the docs for the Andrei's aggregate functions), they felt a little kludgy but potentially very powerful (and confusing if not used well). Interfaces would be a better choice, easy to wrap one's mind around that rather than an undetermined dynamic binding.