Home > Backend Development > PHP Tutorial > Detailed explanation of Traits_php skills in PHP

Detailed explanation of Traits_php skills in PHP

WBOY
Release: 2016-05-16 20:09:25
Original
993 people have browsed it

PHP is a single inheritance language. Before the emergence of PHP 5.4 Traits, PHP classes could not inherit properties or methods from two base classes at the same time. The combination function of PHP's Traits is similar to that of the Go language. You use the use keyword in the class to declare the name of the Trait to be combined. However, the declaration of a specific Trait uses the trait keyword, and the Trait cannot be instantiated directly. Please see the code below for specific usage:

<&#63;php
  trait Drive {
    public $carName = 'trait';
    public function driving() {
      echo "driving {$this->carName}\n";
    }
  }
  class Person {
    public function eat() {
      echo "eat\n";
    }
  }
  class Student extends Person {
    use Drive;
    public function study() {
      echo "study\n";
    }
  }
  $student = new Student();
  $student->study();
  $student->eat();
  $student->driving();
Copy after login

The output results are as follows:

study
eat
driving trait
Copy after login

In the above example, the Student class has the eat method by inheriting Person, and the driving method and attribute carName by combining Drive.

If there is a property or method with the same name in Trait, base class and this class, which one will be retained in the end? Test it with the following code:

<&#63;php 
  trait Drive {
    public function hello() {
      echo "hello drive\n";
    }
    public function driving() {
      echo "driving from drive\n";
    }
  }
  class Person {
    public function hello() {
      echo "hello person\n";
    }
    public function driving() {
      echo "driving from person\n";
    }
  }
  class Student extends Person {
    use Drive;
    public function hello() {
      echo "hello student\n";
    }
  }
  $student = new Student();
  $student->hello();
  $student->driving();


Copy after login

The output results are as follows:

hello student
driving from drive
Copy after login

So the conclusion is: when a method or property has the same name, the method in the current class will override the trait's method, and the trait's method will override the method in the base class.

If you want to combine multiple Traits, separate the Trait names by commas:

use Trait1, Trait2;

What happens if multiple Traits contain methods or properties with the same name? The answer is that when multiple combined Traits contain properties or methods with the same name, they need to be explicitly declared to resolve conflicts, otherwise a fatal error will occur.

<&#63;php
trait Trait1 {
  public function hello() {
    echo "Trait1::hello\n";
  }
  public function hi() {
    echo "Trait1::hi\n";
  }
}
trait Trait2 {
  public function hello() {
    echo "Trait2::hello\n";
  }
  public function hi() {
    echo "Trait2::hi\n";
  }
}
class Class1 {
  use Trait1, Trait2;
}


Copy after login

The output results are as follows:

PHP Fatal error: Trait method hello has not been applied, because there are collisions with other trait methods on Class1 in ~/php54/trait_3.php on line 20
Copy after login

Use insteadof and as operators to resolve conflicts. Insteadof uses a method to replace another, while as gives an alias to the method. Please see the code for specific usage:

<&#63;php
trait Trait1 {
  public function hello() {
    echo "Trait1::hello\n";
  }
  public function hi() {
    echo "Trait1::hi\n";
  }
}
trait Trait2 {
  public function hello() {
    echo "Trait2::hello\n";
  }
  public function hi() {
    echo "Trait2::hi\n";
  }
}
class Class1 {
  use Trait1, Trait2 {
    Trait2::hello insteadof Trait1;
    Trait1::hi insteadof Trait2;
  }
}
class Class2 {
  use Trait1, Trait2 {
    Trait2::hello insteadof Trait1;
    Trait1::hi insteadof Trait2;
    Trait2::hi as hei;
    Trait1::hello as hehe;
  }
}
$Obj1 = new Class1();
$Obj1->hello();
$Obj1->hi();
echo "\n";
$Obj2 = new Class2();
$Obj2->hello();
$Obj2->hi();
$Obj2->hei();
$Obj2->hehe();


Copy after login

The output results are as follows:

Trait2::hello
Trait1::hi

Trait2::hello
Trait1::hi
Trait2::hi
Trait1::hello

Copy after login

The as keyword has another use, which is to modify the access control of the method:

<&#63;php
  trait Hello {
    public function hello() {
      echo "hello,trait\n";
    }
  }
  class Class1 {
    use Hello {
      hello as protected;
    }
  }
  class Class2 {
    use Hello {
      Hello::hello as private hi;
    }
  }
  $Obj1 = new Class1();
  $Obj1->hello(); # 报致命错误,因为hello方法被修改成受保护的
  $Obj2 = new Class2();
  $Obj2->hello(); # 原来的hello方法仍然是公共的
  $Obj2->hi(); # 报致命错误,因为别名hi方法被修改成私有的
Copy after login

Trait can also be combined with Trait. Trait supports abstract methods, static properties and static methods. The test code is as follows:

<&#63;php
trait Hello {
  public function sayHello() {
    echo "Hello\n";
  }
}
trait World {
  use Hello;
  public function sayWorld() {
    echo "World\n";
  }
  abstract public function getWorld();
  public function inc() {
    static $c = 0;
    $c = $c + 1;
    echo "$c\n";
  }
  public static function doSomething() {
    echo "Doing something\n";
  }
}
class HelloWorld {
  use World;
  public function getWorld() {
    return 'get World';
  }
}
$Obj = new HelloWorld();
$Obj->sayHello();
$Obj->sayWorld();
echo $Obj->getWorld() . "\n";
HelloWorld::doSomething();
$Obj->inc();
$Obj->inc();
Copy after login

The output results are as follows:

Hello
World
get World
Doing something
1
2
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone’s study.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template