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

Tech.MyBuilder.com:
Dependency Injection tutorial
May 06, 2015 @ 11:16:25

If you've heard the term "dependency injection" but are still a little fuzzy on the concept, you should check out this new post to the MyBuilder.com site introducing you to some of the basic concepts and using it in some PHP-based examples. They focus on the use of a dependency injection container in this tutorial (versus something like constructor or setter injection).

A lot has been written about dependency injection or DI (for short) in PHP, but it's a bit difficult to find a tutorial showing how a DI container actually works. The objective of this post is to show the internals and concepts of a DI container.

They start with a brief look at what a dependency injection container is and how it's different from a service locator or regular autoloading. They mention some of the benefits it provides and get into the simple example. They start with a class that creates a hard dependency in the constructor and show how to refactor that out to constructor injection. They then take the next step and replace the constructor injection with a DI container injection and how its internals are set up to provide new instances of requested objects (via a services definition).

0 comments voice your opinion now!
dependency injection di container tutorial introduction

Link: http://tech.mybuilder.com/dependency-injection-tutorial/

Rob Allen:
Routing to a controller with Slim
February 18, 2015 @ 10:19:08

Rob Allen has a new post today sharing an extension for Slim he's written to make working with controllers easier.

In a couple of projects that I've written using Slim Framework, I've found it beneficial to organise my code into controllers with injected dependencies; probably because that's how I'm used to working with ZF2. To make this easier, I've written an extension to the main Slim class and packaged it into rka-slim-controller which will dynamically instantiate controllers for you for each route.

His library makes it easy to define controller "paths" instead of the typical closures Slim requires to direct the request to a controller file. He gives several example routes, the code that the controller needs and shows how you can inject objects into the constructor of the controller (dependency injection).

0 comments voice your opinion now!
slimframework slim library route controller dependency injection

Link: http://akrabat.com/slim-framework/routing-to-a-controller-with-slim-framework/

SitePoint Web Blog:
On Our Radar PHP 7 Controversy and Dependency Injection
February 17, 2015 @ 09:08:39

The SitePoint Web blog has a recent post with two things that are on the radar when it comes to PHP - the upcoming PHP version and the practice of dependency injection.

To change things up a bit, we're going to start bringing to you items and information from those discussions that have caught our attention. Sometimes these discussions will be useful and interesting, and sometimes they may be challenging or insightful. Either way, they're likely to bring new information to light that you haven't come across before, and will help to provide insight and perspective on topics you're interested in.

He starts with an overview of the controversy surrounding PHP 7 including its name, feature removal and links to some responses to the proposed changes. The second topic, dependency injection, how it might be evil and some of the opinions that have been expressed around it.

0 comments voice your opinion now!
php7 controversy dependency injection di version

Link: http://www.sitepoint.com/radar-php-7-controversy-dependency-injection-troubles/

Laravel News:
5 Resources to Learn about the Laravel IoC Container
January 02, 2015 @ 10:04:59

The Laravel News site has a post today linking to five handy resources you can use to learn about the Laravel IoC (inversion of control, dependency injection) container.

The Laravel IoC container is a powerful tool for managing class dependencies. It is widely used in Laravel and an important tool for your arsenal. The community has created several tutorials for this and here are five resources that will teach you all about it. [...] By reading these tutorials you'll be up to speed in no time on the Laravel IoC container and also improve your code by implementing it in your application.

As a "bonus" there's also a link to a video narrated by Laravel creator Taylor Otwell himself about the IoC container and its use.

0 comments voice your opinion now!
laravel inversion control ioc container dependency injection tutorials

Link: https://laravel-news.com/2014/12/5-resources-learn-laravel-ioc-container/

Acim.net:
Trait injection in Zend Framework 2
December 11, 2014 @ 11:55:56

Boban Acimovic has recently posted a tutorial showing you how to use traits in a Zend Framework 2 application to inject additional functionality into your pre-existing classes.

There are several tutorials on the Internet which explain how to use interface based dependency injection in Zend Framework 2. The idea is to make an initializer, figure out which interfaces a class implements and then inject appropriate dependencies using setters defined in the interfaces. Bad part about this is that in each class you implement such an interface you have to declare a property which would hold the injected object and also to implement the setter for it, which is defined in the interface, by the way. In order to simplify this further it is possible to write trait for each interface, but then why should not use just traits? Why do we need interfaces? Is this possible at all?

He includes some example code showing how to set up dependency injection for the traits (via a custom injector based on the "InitializerInterface") and make the autoloading easier. He shows how to add this to the provider configuration as an "initializer" and create the first example trait, a checker for data in user passwords. He then drops the functionality into a service class just by using the "use" keyword and the trait name.

0 comments voice your opinion now!
trait injection zendframework2 tutorial dependencyinjection service provider

Link: http://www.acim.net/2014/11/trait-injection-in-zend-framework-2/

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/


Community Events

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


podcast php7 conference composer laravel series application library introduction performance interview install symfony2 opinion api configure framework example community release

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