


A way to achieve code reuse in PHP Traits new features, traits new features_PHP tutorial
A way to achieve code reuse in PHP traits new features, traits new features
I came across traits while reading the yii2 source code, so I studied it and wrote a blog to record it.
Since PHP 5.4.0, PHP implements a method of code reuse called traits.
Traits is a code reuse mechanism for single-inheritance languages like PHP. Traits are designed to reduce the constraints of single-inheritance languages and allow developers to freely reuse method sets in independent classes within different hierarchies. The semantics of traits and class composition define a way to reduce complexity and avoid the typical problems associated with traditional multiple inheritance and mixins.
Trait is similar to a class, but is only designed to combine functionality in a fine-grained and consistent way. Trait cannot be instantiated by itself. It adds a combination of horizontal features to traditional inheritance; that is, members of application classes do not need to be inherited.
Trait example
trait ezcReflectionReturnInfo {
Function getReturnType() { /*1*/ }
Function getReturnDescription() { /*2*/ }
}
class ezcReflectionMethod extends ReflectionMethod {
Use ezcReflectionReturnInfo;
/* ... */
}
class ezcReflectionFunction extends ReflectionFunction {
Use ezcReflectionReturnInfo;
/* ... */
}
?>
Priority
Members inherited from the base class are overridden by members inserted by the trait. The order of precedence is that members from the current class override the trait's methods, and the trait overrides the inherited methods.
Example of priority
class 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();
?>
The above routine will output: Hello World!
Members inherited from the base class are overridden by the sayHello method in the inserted SayWorld Trait. Its behavior is consistent with the methods defined in the MyHelloWorld class. The order of precedence is that methods in the current class override trait methods, which in turn override methods in the base class.
Another example of priority order
trait HelloWorld {
Public function sayHello() {
echo 'Hello World!';
}
}
class TheWorldIsNotEnough {
Use HelloWorld;
Public function sayHello() {
echo 'Hello Universe!';
}
}
$o = new TheWorldIsNotEnough();
$o->sayHello();
?>
The above routine will output: Hello Universe!
Multiple traits
Separated by commas, list multiple traits in the use statement, which can all be inserted into a class.
Examples of usage of multiple traits
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();
?>
The above routine will output: Hello World!
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 to exclude other methods. The as operator can introduce one of the conflicting methods under another name.
Examples of conflict resolution
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 instead of A;
A::bigTalk instead of B;
}
}
class Aliased_Talker {
Use A, B {
B::smallTalk instead of A;
A::bigTalk instead of B;
B::bigTalk as talk;
}
}
?>
In this example Talker uses traits A and B. Since A and B have conflicting methods, they define using smallTalk from trait B and bigTalk from trait A.
Aliased_Talker uses the as operator to define talk as an alias of B's bigTalk.
Modify method access control
Using as syntax can also be used to adjust the access control of methods.
Example of modifying method access control
trait HelloWorld {
Public function sayHello() {
echo 'Hello World!';
}
}
// Modify the access control of sayHello
class MyClass1 {
Use HelloWorld { sayHello as protected; }
}
//Give the method an alias that changes access control
// The access control of the original sayHello has not changed
class MyClass2 {
Use HelloWorld { sayHello as private myPrivateHello; }
}
?>
Compose trait from trait
Just as classes can use traits, other traits can also use traits. By using one or more traits when a trait is defined, it can combine some or all members of other traits.
Examples of composing traits from traits
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();
?>
The above routine will output: Hello World!
Abstract member of Trait
In order to enforce requirements on the classes used, traits support the use of abstract methods.
Indicates an example of enforcing requirements through abstract methods
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;
}
}
?>
Static member of Trait
Traits can be defined by static members and static methods.
Example of static variable
trait Counter {
Public function inc() {
static $c = 0;
$c = $c + 1;
echo "$cn";
}
}
class C1 {
Use Counter;
}
class C2 {
Use Counter;
}
$o = new C1(); $o->inc(); // echo 1
$p = new C2(); $p->inc(); // echo 1
?>
Example of static method
trait StaticExample {
Public static function doSomething() {
return 'Doing something';
}
}
class Example {
Use StaticExample;
}
Example::doSomething();
?>
Examples of static variables and static methods
trait Counter {
Public static $c = 0;
Public static function inc() {
self::$c = self::$c + 1;
echo self::$c . "n";
}
}
class C1 {
Use Counter;
}
class C2 {
Use Counter;
}
C1::inc(); // echo 1
C2::inc(); // echo 1
?>
Properties
Traits can also define properties.
Example of defining attributes
trait PropertiesTrait {
Public $x = 1;
}
class PropertiesExample {
Use PropertiesTrait;
}
$example = new PropertiesExample;
$example->x;
?>
If the trait defines a property, the class cannot define a property with the same name, otherwise an error will be generated. If the property's definition in the class is compatible with its definition in the trait (same visibility and initial value) then the error level is E_STRICT, otherwise it is a fatal error.
Examples of conflicts
trait PropertiesTrait {
Public $same = true;
Public $different = false;
}
class PropertiesExample {
Use PropertiesTrait;
Public $same = true; // Strict Standards
Public $different = true; // Fatal error
}
?>
Differences in Use
Examples of different uses
namespace FooBar;
use FooTest; // means FooTest - the initial is optional
?>
namespace FooBar;
class SomeClass {
Use FooTest; // means FooBarFooTest
}
?>
The first use is use FooTest for namespace, and FooTest is found. The second use is to use a trait, and FooBarFooTest is found.
__CLASS__ and __TRAIT__
__CLASS__ returns the class name of the use trait, __TRAIT__ returns the trait name
An example is as follows
trait TestTrait {
Public function testMethod() {
echo "Class: " . __CLASS__ . PHP_EOL;
echo "Trait: " . __TRAIT__ . PHP_EOL;
}
}
class BaseClass {
Use TestTrait;
}
class TestClass extends BaseClass {
}
$t = new TestClass();
$t->testMethod();
//Class: BaseClass
//Trait: TestTrait
Trait singleton
Examples are as follows
trait singleton {
/**
* private construct, generally defined by using class
*/
//private function __construct() {}
Public static function getInstance() {
static $_instance = NULL;
$class = __CLASS__;
return $_instance ?: $_instance = new $class;
}
Public function __clone() {
trigger_error('Cloning '.__CLASS__.' is not allowed.',E_USER_ERROR);
}
Public function __wakeup() {
trigger_error('Unserializing '.__CLASS__.' is not allowed.',E_USER_ERROR);
}
}
/**
* Example Usage
*/
class foo {
Use singleton;
Private function __construct() {
$this->name = 'foo';
}
}
class bar {
Use singleton;
Private function __construct() {
$this->name = 'bar';
}
}
$foo = foo::getInstance();
echo $foo->name;
$bar = bar::getInstance();
echo $bar->name;
Call trait method
Although it is not obvious, if the Trait method can be defined as a static method in a normal class, it can be called
Examples are as follows
trait Foo {
Function bar() {
return 'baz';
}
}
echo Foo::bar(),"\n";
?>
Are you familiar with the new features of traits? I hope this article can be helpful to you.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
