note 100912 added to language.oop5.inheritance
| From: | OZ at osu1 dot php dot net | Date: | Sun, 14 Nov 2010 23:24:19 +0000 |
| Subject: | note 100912 added to language.oop5.inheritance | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-174029@lists.php.net to get a copy of this message | ||
Model for Mixins pattern:
<?php
interface IMixinsCaller
{
public function __mixin_get_property($property);
public function __mixin_set_property($property, $value);
public function __mixin_call($method, $value);
}
abstract class MixinsCaller implements IMixinsCaller
{
protected $mixins = array();
public function __call($name, $arguments)
{
if (!empty($this->mixins))
{
foreach ($this->mixins as $mixin)
{
if (method_exists($mixin, $name))
{
return call_user_func_array(array($mixin, $name), $arguments);
}
}
}
trigger_error('Non-existent method was called in class '.__CLASS__.': '.$name,
E_USER_WARNING);
}
public function __mixin_get_property($property)
{
if (property_exists($this, $property))
{
return $this->$property;
}
trigger_error('Non-existent property was get in class '.__CLASS__.':
'.$property, E_USER_WARNING);
}
public function __mixin_set_property($property, $value)
{
if (property_exists($this, $property))
{
return $this->$property = $value;
}
trigger_error('Non-existent property was set in class '.__CLASS__.':
'.$property, E_USER_WARNING);
}
public function __mixin_call($method, $value)
{
if (method_exists($this, $method))
{
return call_user_func_array(array($this, $method), $value);
}
trigger_error('Non-existent method was called in class '.__CLASS__.':
'.$method, E_USER_WARNING);
}
public function AddMixin($mixin)
{
$this->mixins[] = $mixin;
}
}
abstract class Mixin
{
/** @var IMixinsCaller $parent_object */
private $parent_object;
public function __construct(IMixinsCaller $parent_object)
{
$this->parent_object = $parent_object;
}
public function __get($property)
{
return $this->parent_object->__mixin_get_property($property);
}
public function __set($property, $value)
{
return $this->parent_object->__mixin_set_property($property, $value);
}
public function __call($method, $value)
{
return $this->parent_object->__mixin_call($method, $value);
}
}
?>
----
Server IP: 195.54.192.44
Probable Submitter: 127.0.0.1
----
Manual Page -- http://www.php.net/manual/en/language.oop5.inheritance.php
Edit -- https://master.php.net/note/edit/100912
Del: integrated -- https://master.php.net/note/delete/100912/integrated
Del: useless -- https://master.php.net/note/delete/100912/useless
Del: bad code -- https://master.php.net/note/delete/100912/bad+code
Del: spam -- https://master.php.net/note/delete/100912/spam
Del: non-english -- https://master.php.net/note/delete/100912/non-english
Del: in docs -- https://master.php.net/note/delete/100912/in+docs
Del: other reasons-- https://master.php.net/note/delete/100912
Reject -- https://master.php.net/note/reject/100912
Search -- https://master.php.net/manage/user-notes.php