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

Master Zend Framework:
Howto Use Constructor Injection In ZF2
April 15, 2014 @ 12:50:33

The Master Zend Framework site has a new tutorial posted today introducing you to constructor injection in Zend Framework 2 applications, specifically in controller classes.

s it right to use setter injection? Or is it evil, to be avoided at all costs, for the explicitness of constructor injection? In today's post, we explore that and how to implement constructor injection in ZF2 controller classes. Recently on Master Zend Framework, I wrote about using Setter Injection in Zend Framework 2, to supply dependencies to Controller classes.

He talks about the "magic" that can come with frameworks and how constructor injection of the ServiceManager can help clarify and remove some of the problems associated with "magic". He walks you through three steps to getting the ServiceManager injected into the classes:

  • Implement a Class Constructor
  • Initialise your controllers via FactoryInterface
  • Use factories Instead of invokables

He also points out a few benefits to this method of injection, including that it makes the controllers easier to test and the main goal - lack of "magic" in dependencies.

0 comments voice your opinion now!
constructor injection zendframework2 tutorial servicemanager

Link: http://www.masterzendframework.com/tutorial/howto-constructor-injection-in-zf2

SitePoint PHP Blog:
HHVM and Hack - Can We Expect Them to Replace PHP?
February 13, 2014 @ 09:29:39

The SitePoint PHP blog has a new post today that asks can we expect HACK and HHVM to replace PHP as an evolution of the PHP language and interpreter.

HHVM is intended to achieve both parity with the Zend Engine features and best possible performances. Facebook claims a 3x to 10x speed boost and 1/2 memory footprint by switching from PHP+APC to HHVM. Of course this is really application dependent (10x being for the FB code base). [...] Instead this article will focus on HACK which is an evolution of the PHP language designed to be safer, to enable better performance and to improve developer efficiency.

He starts off by helping you get an instance of HHVM up and running (via Vagrant) and create a simple HACK script. From there he gets into some of the more advanced HACK features like constructor argument promotion and collections. The talks some about typing, type hinting and the use of generics as well.

0 comments voice your opinion now!
hack hhvm facebook introduction tutorial type collection constructor

Link: http://www.sitepoint.com/hhvm-hack-part-1/

Allan MacGregor:
Playing with dependency injection in PHP
January 07, 2014 @ 10:08:10

Allan MacGregor has a recent post to his site looking at the use of Dependency Injection in PHP applications. He covers two main methods, constructor injection and setter injection, and code examples of each.

By using Dependency Injection we can write more maintainable, testable, and modular code. All projects have dependencies. The larger the project the more dependencies is it bound to have; now having a great number of dependencies is nothing bad by itself however how those dependencies are managed and maintained is.

Dependency Injection is not a new pattern and it has been commonly used on many languages like Java, but this pattern is somewhat new in the PHP world and it's gaining traction quickly thanks for frameworks like laravel.

He starts with a class that has no dependency injection, hard-coding dependencies inside the constructor. His first refactor shows how to take this class and use constructor injection to pass in an instance of an object rather than a value. After briefly explaining this method, he moves on to setter injection. He shows how to use a "setter" method to inject the needed object.

0 comments voice your opinion now!
dependency injection constructor setter tutorial introduction

Link: http://coderoncode.com/2014/01/06/dependency-injection-php.html

Juan Treminio:
Unit Testing Tutorial Part V Mock Methods and Overriding Constructors
April 05, 2013 @ 09:38:49

Juan Treminio has posted the latest part of his unit testing series to his site today - the fifth part that looks at using mock methods on mock objects and overriding constructors.

Previously in my PHPUnit tutorial series, you learned about the very powerful concept of mock objects and stub methods. This concept is central to successful unit testing, and once it fully 'clicks' in your head you will start to realize how useful and simple testing can be. There is also another thing I want to make clear: creating tests is basically a puzzle - you simply have to go step by step, making sure all the pieces fit together correctly so you can get your green. I hope to make clear what I mean by the end of this tutorial.

He assumes you already know about mock objects and introduces the concept of "stub methods" and "mock methods", noting the difference between them. He then gets into what he calls the "four pathways of getMockBuilder" and talks about the rationale behind mocking methods in the first place. He then gets into constructors and how you can work around the "bad" ones with help from mock object functionality.

If you're interested in reading the rest of the series, you can find links to them here.

0 comments voice your opinion now!
phpunit tutorial mock method object constructor

Link: http://jtreminio.com/2013/03/unit-testing-tutorial-part-5-mock-methods-and-overriding-constructors/

Bob Majdak:
Extending an Iterator to use an Iterator to make your code a little cleaner
March 12, 2013 @ 09:25:04

In this new post to his site Bob Majdak talks about extending iterators to help make it easier to customize it for your needs.

One of the nice things about iterators is the ability to shove them into other iterators, allowing you to wrap their functionality in other functionality to return a more precise result set. Take for example the idea that we want to read a directory to list only the images inside of it. There are two main ways to do this, via readdir functions and via FilesystemIterator objects. Going the FilesystemIterator route, one common way seems to be extend another class called FilterIterator which allows you to customize a filter based on you overwriting a method called accept().

He shows not only overriding the "accept" method, but also the constructor to make using this new iterator a much simpler (and cleaner) call. You can find out more about the FilesystemIterator (and others) over in the Iterators section of the PHP manual.

0 comments voice your opinion now!
extend iterator clean code accept constructor filesystemiterator


Matt Frost:
Dependency Injection Container Question
February 18, 2013 @ 09:26:17

In his latest post Matt Frost takes a look at dependency injection. He thinks out loud about some of the common uses for it but wonders if there's a middle ground for using a DIC and injecting manual dependencies.

The question I have is what if a dependency in one class also has a dependency? To illustrate what I mean, here's an example with some code to follow. [...] I'm not really concerned about the code here as much as I am about the concept that I'm trying to illustrate, in order to use a dependency injection container for this scenario.

In his example code, he shows a "DBAuthMethod" class that extends the "AuthMethod" interface and an "Auth" class that requires an instance of "AuthMethod" as a constructor parameter. He wonders about constructor versus setter injection and thinks that a mix of the two may not be the best structure for the code.

I just can wrap my mind around a scenario where you could ONLY use a DIC, and if you can't use the concept exclusively what benefit is there to using it?

Have any suggestions to share? Let him know - this is a problem more and more developers run into as DIC becomes more widely used.

0 comments voice your opinion now!
dependency injection container manual constructor setter


PHPMaster.com:
Dependency Injection with Pimple
January 29, 2013 @ 09:37:50

On PHPMaster.com there's a new tutorial showing you how to use Pimple (the dependency injection container from the Symfony folks) in your application to manage objects and resources.

In application development, we try to create independent modules so that we can reuse code in future projects. But, it's difficult to create completely independent modules which provide useful functionality; their dependencies can cause maintenance nightmares unless they are managed properly. This is where Dependency Injection comes in handy, as it gives us the ability to inject the dependencies our code needs to function properly without hard coding them into the modules.

They start with a look at the problem with working with "concerete dependencies", ones that are hard-coded into your classes making them not only hard to test but potentially difficult to maintain. They include an example of this (a "SocialFeeds" class and friends) and then one of two ways to fix the situation. They start with using constructor-based injection, injecting the Twitter service into the main feeds object. They also talk about another method - setter-based injection - where the objects are injected via specific methods on the object.

As a third alternative, though, they get to using Pimple to manage the objects, making it easier to inject just the one resource into your classes and extract the objects you need from there. There's also a bit of "advanced" usage of Pimple showing the use of the "share" and "extend" methods.

0 comments voice your opinion now!
dependency injection pimple symfony constructor setter tutorial


PHPMaster.com:
Constructors and the Myth of Breaking the Liskov Substitution Principle
October 08, 2012 @ 11:53:13

On PHPMaster.com there's a new post in a series looking at the SOLID design principles in PHP development. In this new tutorial they try to dispel the myth that constructors break the Liskov Substitution Principle ("L" in "SOLID").

Rants aside, the morale of the story can be boiled down to the following: "Object construction is not part of the contract honored by its implementers". It's easier to grasp concepts by example rather than reading dull theory, so in this article I'll be demonstrating from a practical standpoint how the implementation of different constructors down the same hierarchy isn't a violation of the Liskov Substitution Principle, which is a reason why you shouldn't fall into the temptation of tainting your interfaces with constructors.

He illustrates the point with a simple PDO class that implements a "DatabaseAdapterInterface" interface that defines its own constructor that follows the defaults of the PDO extension. He goes on and changes the constructor for the class a bit to take in an array of config options rather than the DSN/User/Password combo. Inside of this constructor, those values are then taken and pushed into PDO to create the connection. He also suggests one other solution - the injection of a connection object ("ConnectionDefinition") into the constructor instead of the configuration directly.

0 comments voice your opinion now!
solid development liskov substitution principle tutorial constructor


Ralph Schindler's Blog:
PHP Constructor Best Practices And The Prototype Pattern
March 12, 2012 @ 11:26:10

In this new post Ralph Schindler takes a look at the Prototype design pattern and uses it to illustrate some best practices in using constructors in PHP.

If your knowledge of constructors ends with "the place where I put my object initialization code," read on. While this is mostly what a constructor is, the way a developer crafts their class constructor greatly impacts the initial API of a particular class/object; which ultimately affects usability and extensibility. After all, the constructor is the first impression a particular class can make.

He starts at ground level, reintroducing what a constructor is and what it should (and shouldn't) be used for. He talks about constructor overloading, constructor injection, dynamic class extension and using the Prototype pattern to create "an unlimited number of objects of a particular type, with dependencies in tact, each with slight variations." He gives an example with a "DbAdapter" class, showing dynamic class instantiation and how to, using the Prototype method, inject a DbAdapter object and have your class use that instead.

0 comments voice your opinion now!
constructor best practice prototype design pattern


Anthony Ferrara's Blog:
IteratorIterator - PHP Inconsistencies And WTFs
November 01, 2011 @ 12:58:07

Anthony Ferrara has a new post to his blog sharing some inconsistencies with iterators that he discovered as discussed with a fellow developer - why some iterators only accept Iterator arguments and others don't.

We were talking about why some of the SPL Iterators accept only an Iterator as the constructor argument (Such as LimitIterator), and others accept either an Iterator or an IteratorAggregate as the argument (Such as IteratorIterator). Feeling that this would be a useful feature to add (having all of them accept an IteratorAggregate), I opened up the PHP source and started looking at how hard of a change this would be. What I found was... Interesting...

He shares some of the C code he came across in his investigation including a "WTF" moment when he found a case statement for DIT_IteratorIterator in a constructor. Because of some of the logic in this constructor, the inputted iterator is "cast down" to a class. This is shown in a few code examples comparing simple iteration objects and arrays and how it seems to be able to bypass class inheritance to use methods from other classes.

0 comments voice your opinion now!
iterator iteratoriterator wtf constructor optional parameter class



Community Events











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


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

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