News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

PHPMaster.com:
Using Traits in PHP 5.4
February 09, 2012 @ 08:40:50

On PHPMaster.com today there's a new tutorial about using a feature in the upcoming PHP release (5.4) to make it easier to work with more modular code - using traits.

In this article I will discuss traits, a new feature introduced in PHP 5.4 to overcome [issues where multiple inheritance is needed]. The concept of traits itself is nothing new to programming and is used in other languages like Scala and Perl. They allows us to horizontally reuse code across independent classes in different class hierarchies.

Included in the post is example code showing what the use of a trait looks like and a method for creating a Singleton that can spawn instances of two classes. Also included are examples of:

  • using multiple traits at once,
  • traits made up of traits,
  • the importance of order,
  • aliasing to avoid conflicts,
  • reflection
  • and a few other features that come along with their use.
0 comments voice your opinion now!
traits tutorial introduction feature multiple inheritance


blog comments powered by Disqus

Similar Posts

Gareth Heyes: PHP nonalpha tutorial

PHPMaster.com: Taking Advantage of PHP's Built-in Server

Gonzalo Ayuso's Blog: Playing with the new PHP5.4 features

Fabien Potencier's Blog: Create your own framework... on top of the Symfony2 Components (part 1)

Mikko Koppanen's Blog: Pretty Thumbnails


Community Events





Don't see your event here?
Let us know!


library configure unittest release laravel wordpress threedevsandamaybe list code developer install introduction community api framework language series bugfix podcast interview

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework