PHP 使用 Trait 解决 PHP 单继承问题详解

2022-10-07,,,,

本文实例讲述了php 使用 trait 解决 php 单继承问题。分享给大家供大家参考,具体如下:

什么是继承?

继承实际上是存在于面向对象程序设计中的两个类之间的一种关系,是面向对象程序设计方法的一个重要手段,通过继承可以更有效地组织程序结构,明确类间的关系,充分利用已有的类来完成更复杂、更深入的开发。

当一个类拥有另一个类的所有数据和操作时,就称这两个类之间具有继承关系。

被继承的类称为父类,继承了父类的所有数据和操作的类称为子类。

在 php 中用 extends 表明子类与父类的继承关系。

在面向对象的程序设计中,采用继承的方式来组织设计系统中的类,可以提高程序的抽象程度,更接近人的思维方式,使程序结构更清晰并降低编码和维护的工作量。

  • 单继承是指任何一个类都只有一个单一的父类,其结构可以用单纯的树状结构来表示;
  • 多继承是指一个类可以有一个以上的父类,它的静态的数据属性和操作从所有这些父类中继承,其结构应以复杂的网状结构来表示。

php 仅支持单继承,而多继承是通过接口或者 trait 来实现的。

php 的单继承示例:

// 单继承:一个子类只能有一个父类
class a{
  function show() {
    echo "a";
  }
}

class b{
  function show() {
    echo "b";
  }
}

class c extends a{

}

$c = new c;
$c->show();

使用 trait 解决 php 的单继承

自 php 5.4.0 起,php 实现了一种代码复用的方法,称为 trait。

  • trait 是为类似 php 的单继承语言而准备的一种代码复用机制
  • trait 为了减少单继承语言的限制,使开发人员能够自由地在不同层次结构内独立的类中复用 method
  • trait 和 class 组合的语义定义了一种减少复杂性的方式,避免传统多继承和 mixin 类相关典型问题
  • 无法通过 trait 自身来实例化

示例:

<?php
  trait reader{
   public function add($var1,$var2){
     return $var1+$var2;
   }
  }
  trait writer {
   public function multiplication($var1,$var2){
     return $var1*$var2;
   }
  }
  class file {
   use reader;
   use writer;
   public function calculate($var1,$var2){
     echo "ressult of addition:".$this->add($var1,$var2) ."\n";
     echo "ressult of multiplication:".$this->multiplication($var1,$var2);
   }
  }
  $o = new file();
  $o->calculate(5,3);

多个 trait

通过逗号分隔,在 use 声明列出多个 trait,可以都插入到一个类中。

<?php
trait hello {
  public function sayhello() {
    echo 'hello ';
  }
}

trait world {
  public function sayworld() {
    echo 'world';
  }
}

class myhelloworld {
  use hello, world;
  public function sayexclamationmark() {
    echo '!';
  }
}

$o = new myhelloworld();
$o->sayhello();
$o->sayworld();
$o->sayexclamationmark();

冲突的解决

如果两个 trait 都插入了一个同名的方法,如果没有明确解决冲突将会产生一个致命错误。

为了解决多个 trait 在同一个类中的命名冲突,需要使用 insteadof 操作符来明确指定使用冲突方法中的哪一个。

以上方式仅允许排除掉其它方法,as 操作符可以 为某个方法引入别名。 注意,as 操作符不会对方法进行重命名,也不会影响其方法。

示例:

<?php
trait a {
  public function smalltalk() {
    echo 'a';
  }
  public function bigtalk() {
    echo 'a';
  }
}

trait b {
  public function smalltalk() {
    echo 'b';
  }
  public function bigtalk() {
    echo 'b';
  }
}

class talker {
  use a, b {
    b::smalltalk insteadof a;
    a::bigtalk insteadof b;
  }
}

class aliased_talker {
  use a, b {
    b::smalltalk insteadof a;
    a::bigtalk insteadof b;
    b::bigtalk as talk;
  }
}

从 trait 来组成 trait

正如 class 能够使用 trait 一样,其它 trait 也能够使用 trait。在 trait 定义时通过使用一个或多个 trait,能够组合其它 trait 中的部分或全部成员。

<?php
trait hello {
  public function sayhello() {
    echo 'hello ';
  }
}

trait world {
  public function sayworld() {
    echo 'world!';
  }
}

trait helloworld {
  use hello, world;
}

class myhelloworld {
  use helloworld;
}

$o = new myhelloworld();
$o->sayhello();
$o->sayworld();

trait 的抽象成员

为了对使用的类施加强制要求,trait 支持抽象方法的使用。

<?php
trait hello {
  public function sayhelloworld() {
    echo 'hello'.$this->getworld();
  }
  abstract public function getworld();
}

class myhelloworld {
  private $world;
  use hello;
  public function getworld() {
    return $this->world;
  }
  public function setworld($val) {
    $this->world = $val;
  }
}

trait 的静态成员

traits 可以被静态成员静态方法定义。

<?php
// 静态成员 trait counter { public function inc() { static $c = 0; $c = $c + 1; echo "$c\n"; } } class c1 { use counter; } class c2 { use counter; } $o = new c1(); $o->inc(); // echo 1 $p = new c2(); $p->inc(); // echo 1
<?php
// 静态方法
trait staticexample {
  public static function dosomething() {
    return 'doing something';
  }
}

class example {
  use staticexample;
}

example::dosomething();
<?php
// 定义属性
trait propertiestrait {
  public $x = 1;
}

class propertiesexample {
  use propertiestrait;
}

$example = new propertiesexample;
$example->x;

参考:

《PHP 使用 Trait 解决 PHP 单继承问题详解.doc》

下载本文的Word格式文档,以方便收藏与打印。