
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
PHP Traits
Introduction
PHP doesn't support multiple inheritance. This limitation is overcome to some extent by the feature of traits. It is a mechanism of code reuse. Definition of trait is similar to class. However, it can't be instantiated directly. Instead, functionality of a trait is made available to a class by use keyword. Hence, a class can avail methods defined in a trait or can even override them. This is in addition to any other parent class that it might be inheriting.
A trait also is similar to an interface. The difference is that interface doesn't provide definition of method inside it, which must be done by implementing class. Trait method however, does provide definition, which may or may not be overridden by class that uses the trait.
Syntax
<?php trait testtrait{ public function test1(){ //body of method } } //using trait class testclass{ use testtrait //rest of members in class } ?>
Example
In following code, a trait with two methods is used in a class that overrides one of the methods
Example
<?php //definition of trait trait testtrait{ public function test1(){ echo "test1 method in trait
"; } public function test2(){ echo "test2 method in trait
"; } } //using trait class testclass{ use testtrait; public function test1(){ echo "test1 method overridden
"; } } $obj=new testclass(); $obj->test1(); $obj->test2(); ?>
Output
The output is as below −
test1 method overridden test2 method in trait
Trait in child class
This is the main advantage of trait. A class with a parent can also use a trait and may choose to override its method. Thus effectively achieveing multiple inheritance. Example of this feature is given below −
Example
<?php //definition of trait trait testtrait{ public function test1(){ echo "test1 method in trait
"; } } //parent class class parentclass{ public function test2(){ echo "test2 method in parent
"; } } //using trait and parent class class childclass extends parentclass{ use testtrait; public function test1(){ echo "parent method overridden
"; } public function test2(){ echo "trait method overridden
"; } } $obj=new childclass(); $obj->test1(); $obj->test2(); ?>
Output
Above code produces following result −
parent method overridden trait method overridden
Trait with interface
It is possible to have a class implementing an interface, extending other parent class and using trait at the same time
Example
<?php //definition of trait trait mytrait{ public function test1(){ echo "test1 method in trait1
"; } } class myclass{ public function test2(){ echo "test2 method in parent class
"; } } interface myinterface{ public function test3(); } //using trait and parent class class testclass extends myclass implements myinterface{ use mytrait; public function test3(){ echo "implementation of test3 method
"; } } $obj=new testclass(); $obj->test1(); $obj->test2(); $obj->test3(); ?>
Output
This will produce following output −
test1 method in trait1 test2 method in parent class implementation of test3 method
conflict resolution
If a class uses two traits with a common method, their conflict is resolved by scope resolution operator and insteadof keyword
Example
<?php trait trait1{ public function test1(){ echo "test1 method in trait1
"; } public function test2(){ echo "test2 method in trait1
"; } } trait trait2{ public function test1(){ echo "test1 method in trait2
"; } public function test2(){ echo "test2 method in trait2
"; } } //using trait and parent class class testclass { use trait1, trait2{ trait1::test1 insteadof trait2; trait2::test2 insteadof trait1; } } $obj=new testclass(); $obj->test1(); $obj->test2(); ?>
Output
Above script produces following result
test1 method in trait1 test2 method in trait2
- Related Articles
- What is traits in PHP?
- Explain\"Dominant traits\".
- Acquired and Inherited Traits
- How do traits get expressed?
- Personality Traits and Personality Types
- Transgenic Crops and Agronomic Traits
- Tabulate two distinguishing features between acquired traits and inherited traits with one example of each.
- List two differences between acquired traits and inherited traits by giving an example of each.
- The Aries Woman – 10 Personality Traits
- The Leo Man: 10 Personality Traits
- From and Into Traits In Rust Programming
- 7 Weird Traits That Make Men Attractive
- Gene Mapping for Qualitative and Quantitative Traits
- How do Mendel’s experiments show that the(a) traits may be dominant or recessive.(b) traits are inherited independently.
- In the human blood grouping, the four basic blood types are type A, type B, type AB, and type O. The blood proteins A and B are:(a) simple dominant and recessive traits (b) incomplete dominant traits(c) codominant traits (d) sex-linked traits
