News Feed
Sections




News Archive
feed this:

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

Zumba Engineering Blog:
Incorporating Mongounit into Multi-datasource Models with Traits
October 31, 2013 @ 10:42:27

On the Zubma Engineering blog today Chris Saylor has written up a tutorial showing how they used traits to use multiple data sources with Mongounit, working around the single source limitations it enforces.

A while back we open sourced Mongounit, a PHPUnit extension for testing models utilizing mongodb. One key issue that we've discovered as we incorporate MongoDB into more of our data models is that extending Mongounit's TestCase class limits that unit test towards Mongo only as the datasource. Since only a portion of our data is in Mongo while the remaining is in MySQL, limiting a test case to work with one datasource or another is too limiting.

They tried two other solutions first, separating out the tests by data source and manually clear the Mongo data in the tests, but both ran into problems. Instead, they opted to use traits to provide drop-in Mongo testing support as needed. It provides a simple interface to set up and tear down the needed Mongo resources - an example of which is also provided in the post. The code for the trait can be found on Github.

0 comments voice your opinion now!
trait mongounit unittest database multiple source model

Link: http://engineering.zumba.com/2013/10/30/multiple-data-sources-phpunit-testing/

7PHP.com:
Auto Generate Properties Dynamically For Your Classes Using Magic Methods & Reflection
October 28, 2013 @ 12:57:14

Accessing private class properties via getters and setters is a pretty standard way to write your applications. Unfortunately it can be time consuming to write them for every property your class may have. On 7PHP.com Khayrattee Wasseem has a few ideas (including using PHP's own Reflection functionality) to dynamically create them.

When coding a project, at times (or most of it?) some classes might have more than 3 fields (for whatever reason it suits you). So instead of each time writing and repeating setters and getters (accessor methods), I would like to have a piece of reusable code for all my classes without me ever writing a single line of code for accessors. ('ever' as in 'very very rarely'). Now, we also have to take into consideration that some fields might be only get-able or only set-able - (our re-usable piece of code should cater for this)

He shows two different methods to accomplish this kind of dynamic access, one using traits and the other using normal class inheritance. HE includes the code illustration each solution and talks a bit at the end of each section of why that method might be better than the other.

0 comments voice your opinion now!
reflection getter setter private property tutorial trait inheritance

Link: http://7php.com/magic-dynamic-properties/

Cal Evans:
Managing the Verbosity of symfony's Command Object With a Trait
July 15, 2013 @ 12:29:39

In this new post to his site Cal Evans goes through a simple application he's created using the Symfony Command component and some of his gotchas along the way. He shows how, using a trait, he was able to control the output of the command more effectively.

Recently I came up with a project I needed to build. Honestly, it's one of those "not useful to anyone but me" kind of projects. I decided though that while I was at it, I wanted to learn something new. Since I've been a die-hard Zend guy since Zend Framework was 0.2, and more recently a Silex believer, I thought I would drop down a level form Silex and dig into symfony itself. This project is strictly a command line project. No API, no web interface, just a command and a few switches. Digging around I found SymfonyComponentConsoleCommandCommand so I decided to give it a try.

He goes through some of the work he did to pull in the needed packages and create his first command. He creates a "writeln" method to output to the console that contains his check for the verbosity level of the current command options (like "quiet" or "verbose"). He introduces the trait to add some additional handling to this output, making it easier to just drop in the "writeln" functionality in any command of his choosing.

0 comments voice your opinion now!
verbosity trait symfony command silex writeln

Link: http://blog.calevans.com/2013/07/10/managing-the-verbosity-of-symfonys-command-object-with-a-trait

Josh Adell:
Interfaces and Traits A Powerful Combo
September 28, 2012 @ 08:51:16

Josh Adell has a new post today looking at the "powerful combination" of using traits and interfaces in PHP applications. He shows how, despite traits not implementing the interface directly, they can be used to make other classes adhere to them simply by "using" them.

If you're not using interfaces in PHP, you are missing out on a powerful object-oriented programming feature. An interface defines how to interact with a class. By defining an interface and then implementing it, you can guarantee a "contract" for consumers of a class. Interfaces can be used across unrelated classes. And they become even more useful when combined with the new traits feature in PHP 5.4.

He illustrates with a package shipping example and uses an "Addressable" Interface to define the structure for both a Company and Users class. He includes code showing how to implement it in a more traditional "implements" way in a class, but also shows an interesting way to achieve the same thing with traits. Having a trait that follows the interface makes it easy to have a class adhere to the interface just by including the trait (or "using" it).

0 comments voice your opinion now!
interface trait tutorial implement use structure


Zend Developer Zone:
Trait-like Functionality for PHP
April 07, 2010 @ 10:05:52

In a new tutorial posted on the Zend Developer Zone today Steve Hollis looks at something several PHP developers have said they want to seen in the language - traits - and how, until then, you can use traits now (well, sort of).

So why do we need traits, anyway? PHP is a single inheritance language, meaning that each class can only extend one other. This allows us to build logical class hierarchies which extend functionality in a "vertical" way. [...] What we need is a means of re-using code other than simple inheritance. That's where traits come in. Traits are classes containing a collection of methods, usually relating to a particular behaviour.

He shares a way that you can mimic some of the functionality that traits would give you (full code is available here) with an abstract trait class, reflection and a "superclass" that's extended to pass off the requests to the correct child object.

0 comments voice your opinion now!
trait functionality tutorial mimic


Stubbles Blog:
Traits for PHP
February 20, 2008 @ 11:17:00

On the Stubbles blog, Frank Kleine has pointed out a proposal from Stefan Marr to include functionality for traits in PHP.

As a language construct within PHP it would be very easy to do the same, but easier to learn and to use. Beside some keyword issues (the RFC uses "use" as keyword, but this will of course conflict with namespaces) I'm wondering what this will have an impact on reflection - the RFC stays unclear about this.

A trait is a free-floating chink of functionality that, instead of being confined to a class (or file) that has to be called on to use it, exists where the developer can use it from anywhere at any time. This helps solve some of the issues with single inheritance in PHP.

0 comments voice your opinion now!
trait language functionality independent proposal rfc



Community Events





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


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

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