News Feed
Sections




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

Gonzalo Ayuso's Blog:
Inject dependencies via PhpDoc
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.

0 comments voice your opinion now!
phpdocumentor comment tutorial injection traits


blog comments powered by Disqus

Similar Posts

DevShed: Creating the Blog Script for a PHP/MySQL Blogging System (Part 2)

Craig Freshwater\'s Blog: Using PHP for Website Navigation, Explained

Test.ical.ly Blog: Wrapping symfonys functional tests in a PHPUnit test case

Rob Allen's Blog: Using Zend\Loader\Autoloader

Lorna Mitchell's Blog: Gthub API: Issues List


Community Events





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


podcast laravel interview unittest refactor list framework code community threedevsandamaybe configure developer series language opinion testing introduction symfony2 install release

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