News Feed
Sections




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

U
October 02, 2013 @ 10:56:28

For those that might have heard of traits (made available in newer versions of PHP, 5.4+) but haven't seen much of a practical application, this new post from Matthew Setter could help.

Here's the situation which prompted the use of them, in a nutshell. I had a custom view helper which performed some rather elementary date & time formatting, based purely on US standards. When I first wrote the ViewHelper, I wasn't aware of any other use case I'd have for it. So it made sense for it to be self-contained. Such is life however, as later in development the need did arise to do more date/time formatting. But this time, far removed from the view layer in a model. [...] So I weighed up my options and chose to go with Traits. I'll be honest, there was the new & cool factor to them as well - as well as an irresistible sense of simplicity in them.

He shares the actual trait code he implemented, making two simple methods - one for formatting date and another for formatting the time - for his views to use. He also includes examples of it in use. He also sought some feedback

0 comments voice your opinion now!
zendframework2 traits reuse view helper

Link: http://www.maltblue.com/php/using-traits-for-code-reuse-in-zend-framework-2

blog comments powered by Disqus

Similar Posts

Gonzalo Ayuso's Blog: Inject dependencies via PhpDoc

Matthew Weier O'Phinney's Blog: Developing A ZF2 Blog

Project: Patchwork-UTF8 - UTF8 Support for PHP

Matthew Weier O'Phinney's Blog: View Layers, Database Abstraction, Configuration, Oh, My!

Chris Jones: Getting Started with PHP Zend Framework 2 for Oracle DB


Community Events





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


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

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