Traits: Give power to your class

While building application using OOP class, interface play an important role. These two construct are very basic building block of software design. Unlike interface php has new concept called trait.

Trait is nothing new but set of class methods which has implementation details also. But interface have only function declaration not implementation. This makes PHP trait unique from interface.

Let’s take a practial example to understand this awesome feature provided by PHP 5.4 .

See below code which gives small higlight on how to use trait.

You can see that we have used one DriverTrait and used it in multiple class to leaverage same operaitons.

NOTE: This feature is only provided on PHP5.4 and above.

 

Advertisements

3 thoughts on “Traits: Give power to your class

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s