Skip to content

Latest commit

 

History

History
68 lines (53 loc) · 4.06 KB

README.md

File metadata and controls

68 lines (53 loc) · 4.06 KB

Traits In PHP

According to the PHP documentation, Traits are a mechanism for code reuse in single inheritance languages such as PHP. Purposely,Traits are introduced in PHP language to resolve the limitation of single inheritance and to give a flavor of multiple inheritance. Unlike other OOP languages, PHP does not support the concept of multiple inheritance. To be precise, in PHP a child class can not be derived from more than one parent classes. Consequently, Traits are the strategy for playing an important role in PHP to get rid of the situation. Basically, a Trait can be defined by the use of trait keyword and it can be inserted within a class by the use of use keyword. Nevertheless, Traits can not be instantiated by their own during their life time. Last but not least, Traits can define both properties and methods, but they can not define constants.

By the way, I have written a fruitful article on Interfaces, Traits and Abstract Classes, wherein Traits are combined by other PHPs OOP features. The article can be reached by this Link. Additionally, PHPs documentation on Traits can also be found by this Link.

Traits in PHP offer a lot of functionalities like classes, but only some of them I have utilized here to make the examples as simple as purposive. Although I have introduced two examples, but the 2nd one is only a static-version of the 1st, which is a bit shorter one. Nevertheless, both the examples will perform for getting flight-booking tickets. The examples are shown below:

  • traits.php
  • traits_static-version.php

Basically, the structure of a Trait is very similar to that of a class. In the examples below, we have defined FlightServices and FlightInfo as Traits, which contain both non-abstract and abstract methods. Now, from the audience, somebody may ask the question: what is an abstract method? A method that does not obtain any implementation, more specifically, a method that does not hold a body, is defined as abstract method. Additionally, these abstract methods must be implemented by the extending Traits and classes. As it can be seen by the following examples, wherein both sit() and food() abstract methods have been implemented by the Economic and Business classes.

What is the important point in the examples to mention about Traits is that, FlightServices and FlightInfo Traits contain different methods to execute flight bookings for Economic and Business classes. If FlightServices and FlightInfo were classes instead Traits, either the Economic or the Business class would not be able to extend the both (“classes”) simultaneously, since PHP does not support multiple inheritance, where some other OOP languages do. Surprisingly, PHP does allow a class to hold more than one Trait at once, that somewhat reduces the lack of multiple inheritance.

In the following code, by the support of FlightBooking object, initially we can create an object, either it is Economic or Business, and then we can confirm a flight booking by the execution of confirmFlight() method. Overall, a ticket is made by the aid of ticket() method.

//......

class FlightBooking
{    
	//.....
	
	protected function ticket($c)
	{
		//..........
	}

	public function confirmFlight($category)
	{	
		$category = new $category;
		$this->ticket($category);
	}    
}

$f = new FlightBooking();
//.......
$f->confirmFlight($class); // where $class is either 'Economic' or 'Business'

Stars from the audience would be always appreciated

A detail illustration of this topic and some of my other articles on different topics can be reached by the medium blog site.