News Feed
Sections




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

Mayflower Blog:
Traits in PHP 5.4
August 05, 2011 @ 11:07:08

On the Mayflower blog there's a new post looking at the upcoming traits support PHP 5.4 will have to offer. (Note: original post in German)

PHP 5 was a big step in the right direction and today resembles the object-orientation of Java very much. At the end of June 2011 one more step was made, not based on Java, but from another popular language - Scala. The new language feature called Traits allow class and cross-code reuse with no vertical transmission. It is basically a relatively simple mechanism, which is explained below using a simple example.

Their examples are a "before" and "after" of the same functionality - creating a controller that can respond with a JSON message. The first example shows a simple controller that can respond in JSON, a JSON class that can work with HTTP and a User class that implements the Serializable interface on the JSON namespace. The second example redefines the Serializable interface as a trait that can be used inside the User class rather than inherited by it.

There's also a brief look at using multiple traits in one class and what to do about naming conflicts that might pop up and the prioritization method that's been put in place to help.

0 comments voice your opinion now!
traits feature upcoming example tutorial


blog comments powered by Disqus

Similar Posts

Gonzalo Ayuso's Blog: Real-life example of Closure usage with PHP5.3

Fred Muya: Configuring Custom Logging in Laravel 5

RosSoft Blog: Tutorial - Web based search engine for Flickr

Oracle Technology Network: Scaling a PHP MySQL Web Application, Part 1

Evert Pot's Blog: Creating Streams from Strings in PHP


Community Events

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


symfony2 part2 api interview conference configure example podcast series performance community php7 language voicesoftheelephpant release framework opinion application introduction laravel

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