 | News Feed |
 | Jobs Feed |
Sections
|
| feed this: |  |
Gonzalo Ayuso's Blog: Inject dependencies via PhpDoc
by Chris Cornutt April 10, 2012 @ 10:23:14
Gonzalo Ayuso has a new post to his blog looking at a method for injecting dependencies into your application's code based on comments in the PHPDocumentor-formatted comments of your methods.
Last month I attended to Codemotion conference. I was listening to a talk about Java and I saw the "@inject" decorator. I must admit I switched off my mind from the conference and I started to take notes in my notebook. The idea is to implement something similar in PHP. It's a pity we don't have real decorators in PHP. I really miss them. We need to use PhpDoc. It's not the same than real decorators in other programming languages. That's my prototype. Let's go.
All of the code you'll need to recreate his solution is included - a sample "User" class that needs a valid PDO object in a private "db" property, a "DocInject" class that parses the comments and, using a new feature of PHP 5.4 (traits), injects the needed functionality into the "User" class and creates/assigns the object.
You can see just the full code in these two gists on Github.
voice your opinion now!
phpdocumentor comment tutorial injection traits
Refulz Blog: Traits in PHP 5.4 - Why we need Traits
by Chris Cornutt March 30, 2012 @ 11:53:35
On the Refulz blog today there's a new post about traits in PHP (recently introduced in PHP 5.,4) and why we need them in our development.
Traits is one major addition to PHP. We read an introductory article about Traits in PHP 5.4. In the post, we saw an example code which only resembles single inheritance. So, what is the actual purpose of Traits and why do we need to use Traits in our code.
They give a sample use case involving two types of clients, Business and Individual, and how you can use a single Client class and trait to provide address-related functionality.
voice your opinion now!
traits introduction example class tutorial
PHPMaster.com: Using Traits in PHP 5.4
by Chris Cornutt February 09, 2012 @ 08:40:50
On PHPMaster.com today there's a new tutorial about using a feature in the upcoming PHP release (5.4) to make it easier to work with more modular code - using traits.
In this article I will discuss traits, a new feature introduced in PHP 5.4 to overcome [issues where multiple inheritance is needed]. The concept of traits itself is nothing new to programming and is used in other languages like Scala and Perl. They allows us to horizontally reuse code across independent classes in different class hierarchies.
Included in the post is example code showing what the use of a trait looks like and a method for creating a Singleton that can spawn instances of two classes. Also included are examples of:
- using multiple traits at once,
- traits made up of traits,
- the importance of order,
- aliasing to avoid conflicts,
- reflection
- and a few other features that come along with their use.
voice your opinion now!
traits tutorial introduction feature multiple inheritance
PHP.net: PHP 5.4.0 RC6 released
by Chris Cornutt January 24, 2012 @ 07:32:05
The PHP.net has announced the availability of the latest Release Candidate in the PHP 5.4.0 series - PHP 5.4.0 RC6:
The PHP development team announces the 6th release candidate of PHP 5.4. PHP 5.4 includes new language features and removes several legacy (deprecated) behaviours. Windows binaries can be downloaded from the Windows QA site. [...] The 6th release candidate focused on improving traits. Please test them carefully and help us to identify bugs in order to ensure that the release is solid and all things behave as expected.
You can download this latest release from the PHP QA site (Windows binaries) and test it on your local instance/applications. Any and all feedback about issues should be reported to either the QA mailing list or on the bug tracker. A complete list of updates is available in the NEWS file.
voice your opinion now!
release releasecandidate bugfix qa test traits
Gonzalo Ayuso's Blog: Playing with the new PHP5.4 features
by Chris Cornutt November 28, 2011 @ 08:10:51
Gonzalo Ayuso has a new post to his blog today showing some of the experimentation he's done with PHP 5.4 features like the short array syntax, calling methods through arrays and traits.
PHP5.4 it's close and it's time to start playing with the new cool features. I've created a new Virtual Machine to play with the new features available within PHP5.4. I wrote a post with the most exciting features (at least for me) when I saw the feature list in the alpha version. Now the Release Candidate is with us, so it's the time of start playing with them. I also discover really cool features that I pass over in my first review.
Code snippets are included for each example for:
- Class member access on instantiation
- Short array syntax
- Support for Class::{expr}() syntax
- Indirect method call through array
- Callable typehint
- Traits
- Array dereferencing support
He points to this other post for a complete list of what's been added in 5.4.
voice your opinion now!
feature releasecandidate traits example tutorial
Ryan Gantt's Blog: Horizontal reusability with traits in PHP 5.4
by Chris Cornutt August 24, 2011 @ 10:42:42
Ryan Gantt has a new tutorial posted to his blog today looking at one of the features in the upcoming PHP 5.4.x releases - traits. Specifically he looks at the horizontal reusabillity they allow for in your applications.
The ability for a class to inherit from multiple parents is maligned by many, but can be a good thing in some situations. For those working in PHP, multiple inheritance has never been an option; classes are limited to one parent, though they can implement many other datatypes through the use of interfaces. Interfaces can lead to code duplication in improperly-factored inheritance hierarchies. Even in well-architected hierarchies, multiple classes that implement similar methods can contain a lot of overlap.
He starts with a definition of what traits are and where their real usefulness is (as well as what should be the difference between a class and a trait). He gives an example of a typical hierarchy where two classes extend a parent but then they both need the same functionality. Code duplication's not a possibility and inheritance make run into exposure issues. Traits come to the rescue by dropping in just the feature you need when you need it. His example code shows adding some logging to a simple class via a "Logging" trait and a "Singleton trait" example.
voice your opinion now!
horizontal reusability traits singleton logging tutorial introduction
Dave Marshall's Blog: Traits in PHP 5.4 - HelloWorld with Logging Trait
by Chris Cornutt 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.
voice your opinion now!
traits example logging zendframework helloworld
Mayflower Blog: Traits in PHP 5.4
by Chris Cornutt 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.
voice your opinion now!
traits feature upcoming example tutorial
DZone.com: PHP 5.4 features poll the results
by Chris Cornutt August 03, 2011 @ 08:19:43
On DZone.com today Giorgio Sironi has posted the results of a poll taken a little while back concerning what people thought was the best feature of the upcoming PHP 5.4 release.
After two weeks, we have closed the poll among the PHP community of Web Builder Zone to establish which are the most wanted features, which will influence development of applications on PHP 5.4. Hopefully this poll would also shape our focus in tutorials in the future - I personally plan to dedicate more time to the winning features.
Runners up included the removal of magic quotes and strict mode with the top three being (in this order) the upload progress patch, traits and the array improvements leading the pack. You can see the results here.
voice your opinion now!
poll results traits upload progress array improvement
|
Community Events
Don't see your event here? Let us know!
|