News Feed
Sections




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

Dave Marshall's Blog:
Traits in PHP 5.4 - HelloWorld with Logging Trait
August 08, 2011 @ 10:24:36

Dave Marshall is already looking ahead to the next major release of PHP, version 5.4, and one of the features it will include - traits. In this new post to his blog, he introduces a "logging trait" he's come up with to make logging simpler in your (Zend Framework) application.

One of the 'traits' I find myself constantly adding to library files is optional logging of it's behaviour. The library class has it's own log method, that checks to see if the instance has had a logger injected and if so, logs the message. I see this as a perfect candidate for becoming a reusable trait, as I tend to have the same code copy/pasted throughout my library classes. The problem is, according to the rfc, traits aren't supposed to have state/properties, which makes it difficult to have a DI setter method in a trait.

He includes the code for his trait example (noting that it may or may not end up working correctly in the 5.4 final release) that sets a logger on a sample class and logs a few values out to the console. It's a simple example, but it shows you one of the most useful things about traits - the "drop in resuability" nature they allow in your libraries and classes.

0 comments voice your opinion now!
traits example logging zendframework helloworld


blog comments powered by Disqus

Similar Posts

Padraic Brady's Blog: Self-Contained Reusable Zend Framework Modules With Standardised Configurators

Zend Developer Zone: Zend Framework- Coming soon to a Command Line near You!

Padraic Brady's Blog: Zend Framework Contributors Mailing-List Summary; Edition #1 (June 2011)

Evert Pot's Blog: PHPUnit3 : first impressions

Matthew Weier O'Phinney's Blog: Real-time ZF Monitoring via Zend Server


Community Events





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


composer symfony security list framework community conference artisanfiles laravel language interview podcast opinion introduction library release version tool series voicesoftheelephpant

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