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

SitePoint PHP Blog:
Disco with Design Patterns: A Fresh Look at Dependency Injection
Jul 01, 2016 @ 11:26:44

On the SitePoint PHP blog there's a tutorial posted giving you a "fresh look at dependency injection" using the Disco library from bitExpert.

Dependency Injection is all about code reusability. It’s a design pattern aiming to make high-level code reusable, by separating the object creation / configuration from usage.

[...] Our very own Alejandro Gervasio has explained the DI concept fantastically, and Fabien Potencier also covered it in a series. There’s one drawback to this pattern, though: when the number of dependencies grows, many objects need to be created/configured before being passed into the dependent objects. [...] In this article, we’ll demonstrate the concept [of a dependency injection container] further with a newcomer in this field: Disco.

He starts with the installation of the library via Composer and starting up the built-in PHP web server for testing. With that up and running he shows you how to create an instance of the container and set up a sample service with the configuration in the docblock comments. The tutorial then talks about the service scopes, container parameters and shows it in action to create a simple controller handling frontend. There's a bit more about configuration, the container builder functionality and the creation of a response listener when routes are requested.

tagged: tutorial disco designpattern dependency injection di library

Link: https://www.sitepoint.com/disco-with-frameworks-and-design-patterns-a-fresh-look-at-dependency-injection/

TutsPlus.com:
Drupal 8: Properly Injecting Dependencies Using DI
May 20, 2016 @ 09:23:41

On the TutsPlus.com site today there's a new tutorial posted for the Drupal-ers out there showing you the right way to inject dependencies in a Drupal 8 application.

As I am sure you know by now, dependency injection (DI) and the Symfony service container are important new development features of Drupal 8. However, even though they are starting to be better understood in the Drupal development community, there is still some lack of clarity about how exactly to inject services into Drupal 8 classes.

They start by talking about how most of the current examples just show the static injection of dependencies but that that's not the only way. The article shows how to inject other services into existing services via a simple change to the service definitions. They also talk about "non-service classes" and injecting values there as well (including controllers, forms and plugins).

tagged: drupal8 inject dependency container dynamic static tutorial

Link: http://code.tutsplus.com/tutorials/drupal-8-properly-injecting-dependencies-using-di--cms-26314

Zsolt Szend:
Dynamic dependency injection
May 18, 2016 @ 13:32:25

In this new tutorial Zsolt Szende talks about dependency injection and how to handle objects and related needs at runtime rather than the pre-configured method that some injection containers/systems have defined.

In this short article I would like to demonstrate a way to inject dependencies that are not known until runtime. There are many use cases for this and in essence it is about choosing between concrete implementations of some common interface. In object oriented design this is known at the Strategy pattern. The choice itself can be made in various ways, for example via a configuration option or a command line parameter in case of a console command, and I think the dynamic nature of the choice is the most interesting part of the pattern.

The article provides a practical example of an XML/JSON reader pulling information from an external source. A simple interface is defined and two implementation classes put it to use. Then the "command" pattern is used to apply it to an executable script and how injecting a reader type directly overrides the one from the provided option. This is taken a step further and refactored into a "resolver" to determine the best logic to apply based on the input argument.

tagged: dynamic dependency injection xml json reader tutorial resolver

Link: http://pwm.github.io/dynamic-dependency-injection/

Rob Allen:
DI Factories for Slim controllers
Apr 28, 2016 @ 10:38:10

In a recent post to his site Rob Allen shows you how to create dependency injection factories for Slim (v3) controllers.

When using classes for route actions in Slim 3, I recommend using a single class for each route. However you can use a single class for multiple routes. To register a class method to a route you pass a string as the route callable where the class name is separate from method by a colon. Slim will retrieve MyController from the DI container and then call the listAction method using the usual signature. f you don't specify a method, then Slim will see if it treat the class as a callable, so you can implement __invoke() and then register the route.

He shows how to create a factory method as a part of the Slim container for a constructor and inject the correct dependencies. He shows how to register these dependencies as other factory methods inside the DI container and how to pull them back out for injection. He prefers this method as it reduces "hidden dependency" issues and makes the controllers themselves easier to test.

tagged: slim3 slimframework dependency injection container tutorial factory

Link: https://akrabat.com/di-factories-for-slim-controllers/

SitePoint PHP Blog:
Drunk with the Power of Composer Plugins
Mar 28, 2016 @ 13:54:25

The SitePoint PHP blog has a new tutorial for the Composer users out there talking about Composer plugin development and how they can add functionality to this already powerful tool.

Composer is the sharpest tool in the toolbox of the modern PHP developer. The days of manual dependency management are in the distant past, and in their place we have wonderful things like Semver. Things that help us sleep at night, because we can update our dependencies without smashing rocks together.

[...] Even though we use Composer so frequently, there’s not a lot of shared knowledge about how to extend it. [...] Yet, recent changes have made it much easier to develop Composer plugins. [...] So, today I thought we would explore the possibilities of Composer plugin development, and create a fresh bit of documentation as we go.

He walks you through the creation of a simple plugin: one that tracks users and the dependencies they require. He shows you how to create the initial plugin boilerplate and the creation of the addDependencies and activate methods. These grab the dependencies being added and send the information off to a remote site.

tagged: composer plugin tutorial dependency tracking introduction

Link: http://www.sitepoint.com/drunk-with-the-power-of-composer-plugins/

PHP-DI:
Introducing the PHP-DI bridge for Slim
Mar 11, 2016 @ 10:15:24

In this new article on the PHP-DI project site they've shared one of the latest tools they've created to make it easier to integrate PHP-DI with the latest version of the Slim microframework (v3).

Slim 3 was released 3 months ago and it was significant. It is one of the first frameworks to integrate the latest standards and concepts in its core.

[...] While PSR-7 and middlewares are very interesting, what's more useful for us is the use of container-interop. That means Slim 3 can work with any dependency injection container. It is very easy to replace the default container (Pimple) with PHP-DI, but today we are releasing a "PHP-DI - Slim" bridge that goes a little further.

They then show how to use their new bridge to create controllers as services in the container and pass them parameters. The post ends with the instructions on getting the PHP-DI bridge installed and how to use it to replace the default AppSlim creation of your application.

tagged: phpdi dependency injection container slim microframework slimframework v3 bridge

Link: http://php-di.org/news/18-slim-bridge-released.html

Larry Garfield:
Composer vs. Linux Distributions: A Mental Model Battle
Feb 25, 2016 @ 11:41:11

In his latest post Larry Garfield talks about the Composer problem that was recently brought up by the Gentoo linux project and is related to how Composer packages and system-level shared libraries differ.

This is not a new complaint; Other distributions have complained about Composer's impact before. But fundamentally I think the issue stems from having the wrong mental model of how modern PHP works when viewed from a distribution or sysadmin perspective.

In a recent heated GitHub thread, several people referred to PHP "linking" to 3rd party libraries, as if they were shared C libraries. That is simply not the case. Neither "static linking" nor "dynamic linking" really applies to PHP. From a sysadmin perspective, PHP is closer to highly complicated bash scripts than anything else.

Larry starts with a bit of history on the subject, pointing out the two methods most developers used PHP code: copy/pasted from the web or installed via PEAR. He talks about the common issues with both approaches. He then talks about how modern PHP development and Composer related and how, from a sysadmin perspective, Composer is the "compile" step of PHP and only supports static links. He also makes some suggestions to the distribution packagers around how to handle these system-level Composer dependencies (and how to treat it like a "binary" if needed).

The mistake here is trying to treat dependent packages of modern PHP applications like shared libraries. They're not. The community has spoken, and PHP simply doesn't work that way anymore. Fighting that is a losing battle. But by viewing composer as a compiler, distributions can still slot PHP into their typical workflows and get all of the security update ease that they're looking for.
tagged: composer linux distribution mental model shared library system dependency gentoo

Link: http://www.garfieldtech.com/blog/composer-distribution-mental-model

Loïc Faugeron:
The Ultimate Developer Guide to Symfony - Dependency Injection
Feb 25, 2016 @ 10:43:57

Loïc Faugeron has posted another of his "Ultimate Developer Guide" series to his site, this time with a focus on the dependency injection component of the Symfony framework.

In this guide we explore the standalone libraries (also known as "Components") provided by Symfony to help us build applications. We've already seen: HTTP Kernel and HTTP Foundation, event dispatcher, routing and YAML. We're now about to check Dependency Injection, and finally in the next article we'll have a look at Console.

He starts with an introduction to the dependency injection pattern by refactoring a request to an API for its status out into classes with different responsibilities. He includes both the code he started with and what he's refactoring to. He thing brings in the Dependency Injection component, shows how to register the different classes and define a YAML configuration with their relationships. He also includes an examples of how to pass in parameters when injecting services, extending the functionality and compile the functionality into static values.

tagged: ultimate developer guide symfony dependency injection tutorial series

Link: https://gnugat.github.io/2016/02/24/ultimate-symfony-dependency-injection.html

SitePoint PHP Blog:
How to Build Your Own Dependency Injection Container
Feb 12, 2016 @ 12:22:12

The SitePoint PHP blog has posted a tutorial showing you how to build something that's become an integral part of most frameworks and applications practicing modern PHP development: a dependency injection container.

A search for “dependency injection container” on packagist currently provides over 95 pages of results. It is safe to say that this particular “wheel” has been invented.

However, no chef ever learned to cook using only ready meals. Likewise, no developer ever learned programming using only “ready code”. In this article, we are going to learn how to make a simple dependency injection container package.

He walks you through the entire process of creating the container and each of the pieces that make it up:

  • the overall interface for the container (with has/get methods)
  • having it cooperate with the "container interoperability" structure
  • reference handling
  • constructor injection functionality

Code is included the whole way showing you how to create the parts and fit them together to make more of a whole. He ends the post with a look at a few other dependency injection containers and a reference to the repository with the end result of the article.

tagged: dependency injection container tutorial introduction

Link: http://www.sitepoint.com/how-to-build-your-own-dependency-injection-container/

Toptal.com:
True Dependency Injection with Symfony Components
Jan 20, 2016 @ 10:37:39

On the Toptal.com blog there's a recent post about true dependency injection with Symfony between components in your application and only using the dependency injection container for its intended purpose.

Symfony2, a high performance PHP framework, uses Dependency Injection Container pattern where components provide a dependency injection interface for the DI-container. This allows each component to not care about other dependencies. [...] But this means DI-container can be used as a Service Locator.

[...] In this article we will try to build a Symfony2 application without implementing Service Locator pattern. We will follow one simple rule: only DI-container builder can know about DI-container.

They start off by talking about the structure of the dependency injection container and how it relates to the three main types: controller, method and property injections. He then starts in on creating the sample project and requiring only the Symfony DI, configuration and Yaml components. He then creates a ContainerBuilder class and sets up the HttpKernel functionality to pull the response from the container. He then makes a simple controller with a default action that just responds with text. With this working he updates it to pull in an input variable. He then makes updates to the application with changes to the route handing, templating (Twig), Doctrine (database) and tag handling.

tagged: dependency injection di symfony component framework router yaml container tutorial httpkernel

Link: http://www.toptal.com/symfony/true-dependency-injection-symfony-components