News Feed
Sections




News Archive
feed this:

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

SitePoint PHP Blog:
PHP Dependency Injection Container Performance Benchmarks
August 11, 2014 @ 10:15:14

The SitePoint PHP blog has posted the results of some dependency injection container benchmarks they performed on several different DI libraries, some from a few of the major PHP frameworks.

Most frameworks and larger PHP applications utilize a Dependency Injection Container with the goal of a more maintainable codebase. However, this can have an impact on performance. As loading times matter, keeping sites fast is important as ever. Today I'm going to benchmark several PHP Dependency Injection containers to see what their relative performance is like.

The libraries in their list of those tested include PHP-DI, Zend/Di and the Aura.Di component. They compare each libraries against the others based on execution time, memory usage and the number of files required to make things work. The results of each test are shown in the graphs on the rest of the post. It's also broken up into a few different kinds of tests:

  • Test 1 - Create an instance of an object
  • Test 2 - Ignoring autoloading
  • Test 3 - Deep object graph
  • Test 4 - Fetching a Service from the container
  • Test 5 - Inject a service

The results are pretty consistent across all of the tests with certain libraries always performing better than others....but you'll have to read the post to find out those request. The scripts that were used to get the various results are also shared on GitHub if you'd like to take them for a spin on your own.

0 comments voice your opinion now!
dependency injection benchmark performance container

Link: http://www.sitepoint.com/php-dependency-injection-container-performance-benchmarks/

SitePoint PHP Blog:
Dependency Injection with Laravel's IoC
June 05, 2014 @ 11:51:08

The SitePoint PHP blog has a new tutorial posted showing you how to use the Laravel dependency injection container to handle dependencies in you Laravel-based applications. Younes Rafie introduces some of the basic concepts behind dependency injection and the various types to get everyone started on the same level.

As developers, we are always trying to find new ways to write well designed and clean code by adopting new styles, using design patterns, and trying new robust frameworks. In this article we will explore the dependency injection design pattern through Laravel's IoC component and see how it can improve our design.

He includes examples of the three basic types of injection - controller, setter and interface - with brief code examples of their implementation. He goes on to talk about the "Inversion of Control" principle (part of the SOLID set of principles) and how the Laravel dependency injection container helps by binding objects and instances for later retrieval. Code examples for session storage handling (through a MySQL database) are included that are automatically resolved as the class requires them.

0 comments voice your opinion now!
laravel dependency injection container ioc tutorial introduction session mysql

Link: http://www.sitepoint.com/dependency-injection-laravels-ioc

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/

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

Gonzalo Ayuso:
Auto injecting dependencies in PHP objects
March 05, 2014 @ 09:19:38

In his latest post Gonzalo Ayuso shows how you can automatically inject dependencies into your PHP objects with the help of Pimple, a simple dependency injection container.

I must admit I don't really know what's the correct title for this post. Finally I use "Auto injecting dependencies in PHP objects". I know it isn't very descriptive. Let me explain it a little bit. This time I want to automate the Hollywood Principle ("Don't call us, we'll call you"). [...] We need to use Reflection to create our instance and to call our action. Sometimes I need to work with custom frameworks and legacy PHP applications. I've done it in a couple of projects, but now I want to create a library to automate this operation.

He includes a simple example of a "Controller" class that is injected with a "Request" via constructor injection. He refactors this to create the dependency injection container with a "Builder" class) and shows how to fetch the instance of the "Bar" class from it. It's this Builder class he shares on GitHub.

0 comments voice your opinion now!
dependency injection container pimple builder class tutorial

Link: http://gonzalo123.com/2014/03/03/auto-injecting-dependencies-in-php-objects/

Paul Jones:
What Application Layer Does A DI Container Belong In?
February 12, 2014 @ 09:11:17

Paul Jones has a new post to his site today with his thoughts about where dependency injection belongs in the application layer structure.

James fuller asks: "any thoughts about which layer of the application we should be using a DI container like Aura.Di? Highest layer possible?" Twitter is too constrained and ephemeral for a good response, so I'll answer that question here.

Based around his definition of a dependency injection container (and service locator), he suggests that the DI container should reside outside of the normal application structure, possibly created in the bootstrap. He also talks some about class inheritance and the use of dependencies passed through from parent to child classes (and how common practices can break this).

0 comments voice your opinion now!
application layer dependency injection opinion auradi

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

MaltBlue.com:
Easy Setter Injection in Zend Framework 2
January 30, 2014 @ 11:54:40

Matthew Setter has a new post today looking at setter injection of dependencies in a Zend Framework v2 based application. He shows how to do it via ServiceManager-aware interfaces.

For configuring objects, reused throughout the application, I've found it to be nothing short of amazing. With next to no code, one Module configuration setting, along with the magic of OOP, classes are suitably initialized throughout the application, without any hands-on configuration on my part. Whilst Zend Framework 2 is great without this. When you start using setter injection, it becomes so much more. In today's post, I'll take you through an example which uses setter injection to ensure that the AuthService, or authenticated user object is always available to a class and any of its descendants.

He walks you through a basic implementation, showing the creation of the "AuthAwareInterface" interface class and an implementation of it, the "CacheableTable". In the "CacheableTable" there's a setter and getter for the currently authenticated user. Using these he's able to configure the ServiceManager to get the AuthService instance from the service locator and inject it into the class. He also includes a word of warning to be careful with the injection you do use, pointing out that it can lead to difficult to track bugs and issues down the line.

0 comments voice your opinion now!
zendframework2 setter injection servicemanager servicelocator tutorial

Link: http://www.maltblue.com/zend-framework/easy-setter-injection-in-zend-framework-2

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

Paul Jones:
Quicker, Easier, More Seductive Names, Usage, and Intent
December 18, 2013 @ 10:39:05

Paul Jones has updated his "service locators vs dependency injection containers" series with another post to his site today, this time he focuses on implementation not names. He suggests that the difference in naming makes it easy to think they're very different things, so he focuses on implementation rather than just the names.

As the disucussion progressed, it became more clear to me that there really is no significant difference in how Dependency Injection containers and Service Locator containers are written. They are both Inversion of Control (IOC) containers, and are not distinguishable by their code, API, features, etc. (although some may have more or fewer features than others).

As such, the terms Dependency Injection and Service Locator appear to be interchangeable in the sense that a container is a container is a container. The difference in naming comes from how the container is used, not how the container is implemented.

He suggests that one of the main differences is where they are, either inside or outside of a non-Factory object. He circles back around to the names, though, and points out that when developers talk to one another, they need to be speaking the same language. As such, he tries to set this vocabulary for the implementations, separati

0 comments voice your opinion now!
dependency injection service locator implementation naming

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

Paul Jones:
Quicker, Easier, More Seductive How To Tell A DI Container From A Service Locator
December 17, 2013 @ 13:55:11

Paul Jones has continued his posts about dependency injection containers versus service locators in his site with this new post that hopes to make it easier for you to tell the difference between the two.

It is easy to confuse a Dependency Injection container with a Service Locator. They are very similar to each other. The differences are subtle. Indeed, it's even possible to use a Dependency Injection container as a Service Locator, although I think it's difficult to use a Service Locator as a Dependency Injection container. They are both sub-patterns of a more generic pattern called Inversion of Control, and some people confuse the term Dependency Injection with the more generic term Inversion of Control.

He starts off with a few questions you can ask to see which camp the implementation belongs in, mostly revolving around how the objects are fetched. He includes some code samples to help reinforce the point, showing both a service locator and DIC. He's also done some looking around at some of the major DIC implementations and which of the two he sees them as (with a few notes explaining his thoughts).

0 comments voice your opinion now!
service locator dependency injection compare inversionofcontrol

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


Community Events





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


tool language composer introduction opinion mvc package interview framework update release symfony podcast version series laravel voicesoftheelephpant library community security

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