News Feed
Jobs Feed
Sections




News Archive
feed this:

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

Rob Allen:
Injecting dependencies into your ZF2 controllers
April 23, 2014 @ 09:06:27

Rob Allen has a quick new post to his site showing you how to inject dependencies into controllers in a Zend Framework v2 based application.

When starting working with Zend Framework 2, it's common to copy the skeleton and put your controller definitions in module.config.php. The controllers keyword is picked up by the ControllerManager which is an instance of the ServiceManager which means that it creates the controller instance for you when the dispatcher needs it. As it's just a service manager, we configure it to inject the dependencies that the controller needs rather than having to go fishing for them later.

He shows how to write a "getControllerConfig" method to populate two items into the factory call for a "Blog" controller, blog mapper and content mapper classes. He also includes a reminder that ZF2 controllers have no constructor, so dependencies can be more easily injected and used directly in the class.

0 comments voice your opinion now!
dependency injection controller zendframework2 tutorial factory

Link: http://akrabat.com/zend-framework-2/injecting-dependencies-into-your-zf2-controllers/

Allan MacGregor:
Design Patterns in PHP Using Factories
January 21, 2014 @ 09:39:32

Allan MacGregor has a recent post to his site looking at another design pattern (previously he discussed dependency injection), this time it's the use of factories.

The factory pattern is one of the most commonly used Design Patterns, it is useful when we need to choose between several interchangeable classes at runtime. By using factories we are also separating the creation of an object from the actual implementation. We can define a factory as a simple design pattern that give us a convenient way to instantiate objects. A factory is capable of creating different types of objects without necessarily knowing what type of object is actually being created.

He talks some about the problems that factories try to solve and some of the common implementations - simple, abstract and the factory method. He also covers some times when it's useful to use the pattern (including the creation of complex objects). The rest of the post includes sample code showing the creation of a Product class, creating them manually and the refactor over to a factory created method, making it more flexible for products in the future.

0 comments voice your opinion now!
design pattern factory introduction refactor product

Link: http://coderoncode.com/2014/01/19/design-patterns-php-factories.html

Paul Jones:
The Difference Between Factories, Registries, and Service Locators
December 03, 2013 @ 10:55:09

In his past few posts Paul Jones has been looking at dependency injection versus service locators. In this new post he continues on the topic comparing three methods for working with objects - factories, registries and service locators.

In last week's episode of this unexpected series, I said, "Go ahead and use Service Locator, but make sure each Locator contains only one type of object. This condition of restraint will keep a single locator from becoming a menace." [...] The "only one type of object" constraint generated some discussion regarding factories and registries. [...] The differences between factories, registries, and containers (both service locators and dependency injection containers) can be subtle and hard to grasp at first. Here's how I keep them straight.

He breaks them down with a simple definition for each related to how the object is created and its intended purpose. He also includes an example of a service locator he's created and a snippet of example code showing it in use.

Again, I must stress: Service locators can easily get out of control. If you can avoid using a service locator, you should.
0 comments voice your opinion now!
factory servicelocator registry difference example code

Link: http://paul-m-jones.com/archives/4800

NetTuts.com:
The Repository Design Pattern
November 26, 2013 @ 11:53:16

While design patterns are a wider topic than just PHP, the NetTuts.com site has posted a new tutorial looking at the Repository Pattern and uses PHP and PHPUnit to illustrate how the pattern works. They looks at the structure of the pattern at a high level and provide a more "real world" example too.

The Repository Design Pattern, defined by Eric Evens in his Domain Driven Design book, is one of the most useful and most widely applicable design patterns ever invented. Any application has to work with persistence and with some kind of list of items. These can be users, products, networks, disks, or whatever your application is about. If you have a blog for example, you have to deal with lists of blog posts and lists of comments. The problem that all of these list management logics have in common is how to connect business logic, factories and persistence.

They start with an overview of the pattern and some of the problems that it can help to solve. They also briefly mention the Gateway pattern that will be used in the examples to pull information into the Repository. After covering some of the basic concepts, they get into the code (going the TDD route) showing how to manage comments, like from a blog, inside a repository. It implements a "Comment" class, a persistence mechanism (the Gateway) and a Factory class that takes in the comment data and returns a correctly formatted object. Finally, they make the repository class and show how to add and retrieve comments from its internal data set.

0 comments voice your opinion now!
designpattern repository gateway factory persistence tutorial

Link: http://net.tutsplus.com/tutorials/php/the-repository-design-pattern/

DZone.com:
Factory patterns Collaborators Map
October 24, 2012 @ 09:43:02

On DZone.com Giorgio Sironi has a new tutorial looking at the Factory design pattern, specifically the use of a "collaborators map" to create them inside of a dependency injection container.

Like for every library, you should first evaluate if the costs and benefit of integrating [a dependency injection container] are worth it. The alternative is to write your own factory classes, methods and closures: this article explains one of the patterns for building dynamic Factory objects, and as such lowers the cost of the second option. What you know how to do has a lower cost than what you still have to learn, considering risk and implementation time.

He talks about the "old way" of making your own factories to create objects and how the collaborators mapping can replace that. The collaboration mapping is passed in when the object is created and a "create" method is called when the objects (or sub-objects) are needed. He also mentions some of the "easy" and "hard" changes you could make to this setup to expand its functionality.

0 comments voice your opinion now!
factory designpattern collaborator map object create


PHPMaster.com:
An Introduction to Dependency Injection, Service Locators & Factories (Part 1)
June 21, 2012 @ 08:59:15

On PHPMaster.com they've posted the first part of a series looking at something that's become a hot topic in the PHP community over the last months - dependency management and service locators.

From a programmer's perspective, the process of designing easily consumable APIs while still keeping a decent level of decoupling between the involved classes is always a tricky trade-off, as the more expressive the dependencies are, the harder the work is that needs to be done behind the scenes for wiring up the collaborators in their proper sequence.

In this two-part series I'll be doing a quick roundup of some of the most common methodologies that can be employed for managing class dependencies in modern application development, ranging from using service locators and injectable factories, to sinking your teeth into plain vanilla Dependency Injection, and even implementing a naive Dependency Injection Container.

He talks about the "plague of the 'new' operators" and how, through the use of an injected factory object, they could be easily replaced. Code samples are included showing the initial state and the end result, refactored to inject his "SerializerFactoryInterface" into the "FileStorage" class.

0 comments voice your opinion now!
dependency injection service locator factory tutorial series


PHP-Tip-a-Day:
PHP Tutorial The Allegory of The Factory Pattern
June 12, 2012 @ 09:13:24

On the PHP-Tip-a-Day site there's a new post from Greg Bulmash with an allegory about the Factory pattern (design pattern) to relate it to something a bit more practical and introduce some of its core concepts.

If you've read the About This Site page, you'll know that instead of pursuing my passion for computers, I got sidetracked into the arts in college. So when it came time for me to learn and explain the Factory Pattern, I thought it might be fun to express it as an allegory...

His example involves cars, axles and wood to illustrate how the Factory pattern lets you generate objects of different types automatically without having to worry about how they're created. He includes a code example in the form of an interface and a class that implements it (the "Axle" class).

0 comments voice your opinion now!
factory pattern allegory tutorial interface example


Charles Sprayberry's Blog:
DI and global state
January 31, 2012 @ 09:24:47

In response to some of the comments made on his previous post about why you should use dependency injection in your applications, Charles Sprayberry is back with some more concrete examples showing how it all works with some code to back it up.

To help better explain each of the three aspects of DI I discussed in the previous article I'll be going over each more thoroughly and with those code examples requested. I'll be going through each point one at a time as the explanations will likely be of some length compared to the original post.

He starts with the "villain" of the story - the Singleton design pattern, a difficult to test method that lulls you into thinking you're not in the global scope. He talks about the problem of using this approach and how the Factory design pattern can be used to create an alternative. He changes up the example to create a "DbTableFactory" class that can be used to create the objects needed - in this case a "UserTable" object with the connection injected into it at construct time.

0 comments voice your opinion now!
dependency injection di factory singleton global designpattern


PHPMaster.com:
Understanding the Factory Method Design Pattern
December 16, 2011 @ 12:15:54

On PHPMaster.com today there's a new design pattern-focused that introduces you to using the Factory method to create new objects on the fly.

The Factory Method pattern is a design pattern used to define a runtime interface for creating an object. It's called a factory because it creates various types of objects without necessarily knowing what kind of object it creates or how to create it.

Code is included showing how to make creating "Product_*" classes as simple as calling a "build()" method with the type. A slightly more complex situation is also included - building factories inside of factory methods. For more about the Factory design pattern, check out its Wikipedia page (that includes some common uses and some pitfalls to watch out for).

0 comments voice your opinion now!
factory method designpattern tutorial introduction


P
May 30, 2011 @ 09:48:29

Lukas Smith has a suggestion for developers out there who get the idea to start messing with the order of the parameters of PHP objects/methods - play nice. His example is specifically with Exceptions.

This is just a short follow up to a recent tweet of mine. I have seen this repeatedly happen, even to top notch and usually very careful developers (*). I am not sure why this mistake happens so frequently, but quite often you see code that changes the parameter order for custom Exception constructors. I guess it's mostly because in these cases the developer wants to pass some magic parameters that contain the message (and potentially also the code).

He recommends that, if you really do need to change something like the order of the parameters, use something like a factory to handle the reorganization for you.

0 comments voice your opinion now!
extend reorganization factory designpattern



Community Events











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


hhvm hack language facebook install unittest security symfony2 framework component introduction example database package release composer application podcast performance project

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