Home  >  Article  >  Backend Development  >  Detailed example of PHP using Trait to solve PHP single inheritance problem

Detailed example of PHP using Trait to solve PHP single inheritance problem

coldplay.xixi
coldplay.xixiforward
2020-06-09 11:59:462377browse

Detailed example of PHP using Trait to solve PHP single inheritance problem

PHP Detailed explanation of using Trait to solve PHP single inheritance problem

This article mainly introduces the use of PHP Trait solves the problem of PHP single inheritance. It analyzes in detail the relevant operating skills and precautions for using Trait to implement PHP single inheritance in PHP in the form of examples. Friends in need can refer to the following

The examples in this article describe the use of Trait in PHP Solve PHP single inheritance problem. Share it with everyone for your reference, the details are as follows:

What is inheritance?

Inheritance is actually a relationship between two classes that exists in object-oriented programming. It is an important means of object-oriented programming method. Through inheritance, programs can be organized more effectively. Structure, clarify the relationship between classes, and make full use of existing classes to complete more complex and in-depth development.

When one class owns all the data and operations of another class, it is said that there is an inheritance relationship between the two classes.

The inherited class is called the parent class, and the class that inherits all the data and operations of the parent class is called a subclass.

Use extends in PHP to indicate the inheritance relationship between the subclass and the parent class.

In object-oriented programming, using inheritance to organize classes in the design system can improve the abstraction of the program, bring it closer to the human way of thinking, make the program structure clearer, and reduce coding and maintenance. workload.

Single inheritance means that any class has only a single parent class, and its structure can be represented by a simple tree structure; multiple inheritance means that a class can have more than one parent class, and its static The data attributes and operations are inherited from all these parent classes, and their structure should be represented in a complex mesh structure.

PHP only supports single inheritance, while multiple inheritance is implemented through interfaces or Traits.

Example of single inheritance in PHP

// 单继承:一个子类只能有一个父类
class A{
  function show() {
    echo "A";
  }
}
 
class B{
  function show() {
    echo "B";
  }
}
 
class C extends A{
 
}
 
$c = new C;
$c->show();

Use Trait to solve single inheritance in PHP

Since Starting from PHP 5.4.0, PHP implements a method of code reuse called traits.

Trait is a code reuse mechanism prepared for single inheritance languages ​​like PHP. Trait is designed to reduce the limitations of single inheritance languages ​​and enable developers to freely reuse in independent classes within different hierarchies. The semantics of the methodTrait and Class combination define a way to reduce complexity and avoid the typical problems associated with traditional multiple inheritance and Mixin classes that cannot be instantiated by the trait itself

Example:

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

Multiple traits

Separated by commas, multiple traits are listed in the use statement, and they can all be inserted into a class.

<?php
trait Hello {
  public function sayHello() {
    echo &#39;Hello &#39;;
  }
}
 
trait World {
  public function sayWorld() {
    echo &#39;World&#39;;
  }
}
 
class MyHelloWorld {
  use Hello, World;
  public function sayExclamationMark() {
    echo &#39;!&#39;;
  }
}
 
$o = new MyHelloWorld();
$o->sayHello();
$o->sayWorld();
$o->sayExclamationMark();

Conflict resolution

If two traits insert a method with the same name, a fatal error will occur if the conflict is not explicitly resolved.

In order to resolve the naming conflict of multiple traits in the same class, you need to use the insteadof operator to explicitly specify which of the conflicting methods to use.

The above method only allows other methods to be excluded. The as operator can introduce an alias for a method. Note that the as operator does not rename the method, nor does it affect its methods.

Example:

<?php
trait A {
  public function smallTalk() {
    echo &#39;a&#39;;
  }
  public function bigTalk() {
    echo &#39;A&#39;;
  }
}
 
trait B {
  public function smallTalk() {
    echo &#39;b&#39;;
  }
  public function bigTalk() {
    echo &#39;B&#39;;
  }
}
 
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;
  }
}

Composing traits from traits

Just as classes can use traits, other traits can also use traits. By using one or more traits when defining a trait, you can combine some or all members of other traits.

<?php
trait Hello {
  public function sayHello() {
    echo &#39;Hello &#39;;
  }
}
 
trait World {
  public function sayWorld() {
    echo &#39;World!&#39;;
  }
}
 
trait HelloWorld {
  use Hello, World;
}
 
class MyHelloWorld {
  use HelloWorld;
}
 
$o = new MyHelloWorld();
$o->sayHello();
$o->sayWorld();

Abstract members of Trait

In order to enforce requirements on the classes used, traits support the use of abstract methods.

<?php
trait Hello {
  public function sayHelloWorld() {
    echo &#39;Hello&#39;.$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;
  }
}

Static members of Trait

Traits can be defined by static members and static methods.

<?php<br>// 静态成员
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 &#39;Doing something&#39;;
  }
}
 
class Example {
  use StaticExample;
}
 
Example::doSomething();
<?php
// 定义属性
trait PropertiesTrait {
  public $x = 1;
}
 
class PropertiesExample {
  use PropertiesTrait;
}
 
$example = new PropertiesExample;
$example->x;

Recommended tutorial: "PHP Video Tutorial"

The above is the detailed content of Detailed example of PHP using Trait to solve PHP single inheritance problem. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete