Traits in PHP
With the release of PHP 5.4.0 including support for traits we though it would be good to start taking advantage of this new feature. We will also be modifying some of our older classes to use traits.
Our first trait was a simple single method one which is called FormErrorParser($form)
which takes a string as an argument and parses any error tokens contained inside the HTML markup.
It is a very simple function and a specific problem domain so we felt using a trait to provide this function across classes which are otherwise unrelated was the correct approach to use.
|
PHP 5.4.0 |
Defining a Trait
To define a trait in PHP is very simple and similar to how you would write a class, for our example the calling class must define the $this->formErrors array which is a simple array containing a key value pair ('{TOKEN}' => '{ERROR_STRING}') which the parser transverses,
/**
* Form Error Parser
* Provides functionality for parsing form errors, its a simple parser which replaces error tokens (%nameError%) with the corresponding error string.
* @package Traits\FormErrorParser
* @version 1.0.0
* @author C.Elsen
* @copyright © 2000-2013 Chic Computer Consultants Ltd, All Rights Reserved.
* @date: 2013-09-11
*/
trait FormErrorParser {
/**
* Parse Form Errors
* Parser replaces form error tokens with the error string.
* @param string $form The form to parse.
* @return string Returns the form with the error tokens replaced.
*/
protected function parseFormErrors ($form) {
if (count($this->formErrors) > 0) {
foreach ($this->formErrors as $k => $v) {
$form = str_replace('%' . $k . 'Error%', $v, $form);
}
}
$form = preg_replace('/%.*Error%/i', '', $form);
return $form;
}
}
From the above you can see this is a simple usage but we feel a good example of how traits can help with specific fine grained functions.
Using a Trait
To insert a trait into a class you call the use
keyword along with the name of the trait to use so to use the FormErrorParser trait you would so something like this,
use FormErrorParser
protected function doSomthingWithAForm() {
if (empty($_POST['something'])) {
$this->formErrors['example'] = 'This is an example error string';
}
}
For the HTML side of things you can add an error token to you form markup so that it can be replaced by the appropriate error message.
<input type="text" id="something" name="something">%exampleError%
Conclusion
Having another tool in the toolbox is never a bad thing and traits look like they will allow you to share functionality between classes with no other relationship this should allow for greater code reuse and in turn improve development.
The Care Homes Directory is currently in the process of redesigning our enhanced listings sign up form in an attempt to simplify it and also switch over to Google Wallet from Google Checkout.
Our initial use of Google Wallet is positive and we feel that it is an improvement over Checkout and has a simpler API and better support for digital good and especially subscriptions.
We will be writing a future article regarding the release of this new improved website feature and we hope to make our site as simple as possible.