美文网首页
PHP Traits

PHP Traits

作者: Yves_lau | 来源:发表于2015-06-04 22:16 被阅读108次

(http://php.net/manual/en/language.oop5.traits.php#language.oop5.traits)
As of PHP 5.4.0, PHP implements a method of code reuse called Traits.
Traits are a mechanism for code reuse in single inheritance languages such as PHP. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies. The semantics of the combination of Traits and classes is defined in a way which reduces complexity, and avoids the typical problems associated with multiple inheritance and Mixins.
A Trait is similar to a class, but only intended to group functionality in a fine-grained and consistent way. It is not possible to instantiate a Trait on its own. It is an addition to traditional inheritance and enables horizontal composition of behavior; that is, the application of class members without requiring inheritance.
Example #1 Trait example

<?php
trait ezcReflectionReturnInfo {   
 function getReturnType() { /*1*/ }   
 function getReturnDescription() { /*2*/ }}
class ezcReflectionMethod extends ReflectionMethod {    
     use ezcReflectionReturnInfo;    /* ... */}
class ezcReflectionFunction extends ReflectionFunction {   
 use ezcReflectionReturnInfo;    /* ... */}
?>

Precedence
An inherited member from a base class is overridden by a member inserted by a Trait. The precedence order is that members from the current class override Trait methods, which in turn override inherited methods.
Example #2 Precedence Order Example
An inherited method from a base class is overridden by the method inserted into MyHelloWorld from the SayWorld Trait. The behavior is the same for methods defined in the MyHelloWorld class. The precedence order is that methods from the current class override Trait methods, which in turn override methods from the base class.

<?phpclass 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();?>

相关文章

  • PHP Traits

    (http://php.net/manual/en/language.oop5.traits.php#langua...

  • PHP中的Traits详解

    PHP中的Traits详解 原文链接:http://tabalt.net/blog/php-tr... PHP是单...

  • PHP中的Traits用法详解

    PHP是单继承的语言,在PHP 5.4 Traits出现之前,PHP的类无法同时从两个基类继承属性或方法。php的...

  • thinkphp5.0 助手函数大全

    load_trait:快速导入Traits,PHP5.5以上无需调用 exception:抛出异常处理 debug...

  • PHP traits (复用机制)

    最近在学习Laravel中,遇到了很多关于Traits ,查了下资料,分享下。 提到 php 的代码复用,我们可能...

  • 「php化整为零系列」九、Traits

    该系列我只写我的理解,非官方解释,如不够专业请见谅 PHP是单继承的语言,在PHP 5.4 Traits出现之前,...

  • PHP特性 Traits的引入

    作为一种扩展机制,可以方便的实现一个类库的多继承问题。trait是一种为类似 PHP 的单继承语言而准备的代码复用...

  • PHP 面向对象 (十)traits

  • trait学习

    trait php是单继承,不能多继承traits可以弥补这一点,实现一种类似多继承的集合。

  • STL——迭代器Traits

    Traits应用在很多地方,不仅仅有迭代器;还有: type traits iterator traits cha...

网友评论

      本文标题:PHP Traits

      本文链接:https://www.haomeiwen.com/subject/mcvdqttx.html