我了解到混入(mixins)。我的疑问是,是否可以在PHP中使用混入?如果是的话,该如何使用?
使用 PHP 5.4 中引入的Trait
<?php
class Base {
public function sayHello() {
echo 'Hello ';
}
}
trait SayWorld {
public function sayHello() {
parent::sayHello();
echo 'World!';
}
}
class MyHelloWorld extends Base {
use SayWorld;
}
$o = new MyHelloWorld();
$o->sayHello();
?>
输出Hello World!
的代码如下:
截至 PHP 5.4,此答案已过时。请查看Jeanno的回答以了解如何使用 traits。
取决于您希望从PHP获得什么级别的混合(mixins)。PHP处理单一继承和抽象类,这可以让您实现大部分功能。
当然,混入最好的部分是它们是可互换的代码片段,可以添加到任何需要它们的类中。
要解决多重继承问题,您可以使用include
来导入代码片段。在某些情况下,您可能需要添加一些样板代码才能使其正常工作,但这肯定会有助于保持程序DRY(不重复)。
示例:
class Foo
{
public function bar( $baz )
{
include('mixins/bar');
return $result;
}
}
class Fizz
{
public function bar( $baz )
{
include('mixins/bar');
return $result;
}
}
虽然没有像 class Foo mixin Bar
这样直接的方式来定义一个类,但以下方法可以基本实现相同效果。需要注意的是,这种方法也有一些缺点:你需要保持相同的参数名和返回变量名,还需要传递其他依赖于上下文的数据,例如func_get_args_array
或__FILE__
。
“php5 mixin” 的第一个谷歌搜索结果:http://www.sitepoint.com/forums/php-application-design-147/ruby-like-mixins-php5-332491.html
“php mixin” 的第一个谷歌搜索结果:http://www.advogato.org/article/470.html
简短的回答:是的,但目前还不支持原生(正如 @mchl 所指出的那样)。请查看上述链接。
更长的回答:如果您正在使用runkit,请查看runkit_method_copy()
函数:“从一个类复制一个方法到另一个类。”
我基于在jansch.nl发现的博客条目来实现了Mixin功能。
class Node
{
protected $__decorator_lookup = array();
public function __construct($classes = array())
{
foreach($classes as $class)
if (class_exists($class))
{
$decorator = new $class($this);
$methods = get_class_methods($decorator);
if (is_array($methods))
foreach($methods as $method)
$this->__decorator_lookup[strtolower($method)] = $decorator;
}
else
trigger_error("Tried to inherit non-existant class", E_USER_ERROR);
}
public function __get($name)
{
switch($name)
{
default:
if ($this->__decorator_lookup[strtolower($name)])
return $this->__call($name);
}
}
public function __call($method, $args = array())
{
if(isset($this->__decorator_lookup[strtolower($method)]))
return call_user_func_array(array($this->__decorator_lookup[strtolower($method)], $method), $args);
else
trigger_error("Call to undefined method " . get_class($this) . "::$method()", E_USER_ERROR);
}
public function __clone()
{
$temp = $this->decorators;
$this->decorators = array();
foreach($temp as $decorator)
{
$new = clone($decorator);
$new->__self = $this;
$this->decorators[] = $new;
}
}
}
class Decorator
{
public $__self;
public function __construct($__self)
{
$this->__self = $__self;
}
public function &__get($key)
{
return $this->__self->$key;
}
public function __call($method, $arguments)
{
return call_user_func_array(array($this->__self, $method), $arguments);
}
public function __set($key, $value)
{
$this->__self->$key = $value;
}
}
class Pretty extends Decorator
{
public function A()
{
echo "a";
}
public function B()
{
$this->b = "b";
}
}
$a = new Node(array("Pretty"));
$a->A(); // outputs "a"
$a->B();
echo($a->b); // outputs "b"
编辑:
unset($this->__self->someValue);
这样的事情不会在 Node
上取消设置值。不知道为什么,理论上应该可以工作。有趣的是,unset($this->__self->someValue); var_dump(isset($this->__self->someValue));
将正确地产生 false
,但是从 Node
范围(如 Node->someValue
)访问该值仍将产生 true
。这里有一些奇怪的巫术。