php中可以使用mixin吗
我开始了解mixins。所以我的疑问是,是否可以在php中使用mixins?如果是,那么如何?
I came to know about mixins.So my doubt is, is it possible to use mixins in php?If yes then how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 PHP 5.4 中引入的
Trait
打印
Hello World!
http://php.net/manual/en/language.oop5.traits.php
Use
Trait
introduced in PHP 5.4which prints
Hello World!
http://php.net/manual/en/language.oop5.traits.php
从 PHP 5.4 开始,这个答案已过时。 请参阅 Jeanno 的回答,了解如何使用特征。
这实际上取决于您想要从 PHP 获得什么级别的
mixins
。 PHP 处理单继承和抽象类,这可以帮助您大部分工作。当然,mixin 最好的部分是它们是可互换的片段,添加到任何需要它们的类中。
要解决多重继承问题,您可以使用
include
来引入代码片段。在某些情况下,您可能需要转储一些样板代码才能使其正常工作,但这肯定有助于保持程序干燥。示例:
它不像将类定义为
class Foo mixin Bar
那样直接,但它应该可以帮助您完成大部分工作。有一些缺点:您需要保留相同的参数名称和返回变量名称,您需要传递依赖于上下文的其他数据,例如func_get_args_array
或__FILE__
。This answer is obsolete as of PHP 5.4. See Jeanno's answer for how to use traits.
It really depends on what level of
mixins
you want from PHP. PHP handles single-inheritance, and abstract classes, which can get you most of the way.Of course the best part of mixins is that they're interchangeable snippets added to whatever class needs them.
To get around the multiple inheritance issue, you could use
include
to pull in snippets of code. You'll likely have to dump in some boilerplate code to get it to work properly in some cases, but it would certainly help towards keeping your programs DRY.Example:
It's not as direct as being able to define a class as
class Foo mixin Bar
, but it should get you most of the way there. There are some drawbacks: you need to keep the same parameter names and return variable names, you'll need to pass other data that relies on context such asfunc_get_args_array
or__FILE__
.PHP 的 Mixins (PHP 本身并不实现 Mixins,但这个库会有所帮助)
Mixins for PHP (PHP does not implement Mixins natively, but this library will help)
“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()
: "复制一个方法从一个班级到另一个班级。”First google result for "php5 mixin": http://www.sitepoint.com/forums/php-application-design-147/ruby-like-mixins-php5-332491.html
First google result for "php mixin": http://www.advogato.org/article/470.html
Short answer: yes, but not natively (yet, evidently, as @mchl notes). Check those out.
Longer answer: if you're using runkit, checkout
runkit_method_copy()
: "Copies a method from class to another."我基于 jansch.nl。
编辑:
unset($this->__self->someValue);
这样的操作不会取消Node
上的值。不知道为什么,理论上它应该有效。很有趣unset($this->__self->someValue); var_dump(isset($this->__self->someValue));
将正确生成false
,但是从Node
范围访问该值(如 < code>Node->someValue) 仍会产生true
。那里有一些奇怪的巫毒。I based mixins functionality on the blog entry found at jansch.nl.
EDIT:
unset($this->__self->someValue);
won't unset the value onNode
. Don't know why, as in theory it should work. Funny enoughunset($this->__self->someValue); var_dump(isset($this->__self->someValue));
will produce correctlyfalse
, however accessing the value fromNode
scope (asNode->someValue
) will still producetrue
. There's some strange voodoo there.