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

Nikola Poša:
Using DIC the right way
Sep 05, 2017 @ 10:24:31

In a new post to his site Nikola Poša looks at dependency injection containers and shares what he thinks is the right way to use them in your applications.

DIC stands for Dependency Injection Container, which is a tool that manages the construction and wiring up of application services. It closely relates to the letter "D" of a SOLID acronym - Dependency Inversion Principle and is employed to facilitate adhering to the principle.

By their nature, DI Containers are also Service Locator implementations, design pattern that is the exact opposite to Dependency Injection. Because of that, DI Container is a double-edged sword which can mislead you if not used wisely, and ironically bring your code into a state in which there is no dependency injection at all.

He starts off by talking about two kinds of code in an application: core versus assembly. In this case "core" code is the piece of the application that are then used by "assembly" code to make things happen. He suggests that the DIC shouldn't leak into the core and should be put behind a separation between the core code and assembly code. He includes some sample code illustrating what he means and the idea of splitting out the DIC configuration to help that layer clean.

tagged: dependency injection container tutorial core assembly code abstraction leak

Link: http://blog.nikolaposa.in.rs/2017/09/03/using-dic-the-right-way/

BitExpert Blog:
Why using code as DI config is a win!
Jul 26, 2017 @ 10:58:21

In a post to the bitExpert.de site Stephan Hochdörfer explains why he thinks that using code over configuration in a DI container is a better approach than static configuration definitions.

In my recent talk on introducing Disco - the DI container with the damn coolest name(tm) - I talk about why I believe that using XML or any other non-code configuration (YAML, JSON, ...) is not a good idea. This stirred some twitter discussion recently which led to this blog post.

Just for the record, for a very long time I was part of the XML camp - just browse my collection of old talks to see for yourself. I praised XML a lot as being the only true DI configuration format.

He then goes through some of the main issues he sees with using something like XML for the dependency container's configuration:

  • An XML editor won't give you code-completion for PHP classes or methods.
  • Refactoring won't work properly in an XML configuration file.
  • An XML editor is not capable of doing proper type checks.
  • XML is just too verbose.

For each item he provides a brief explanation and an example of XML where it helps to illustrate the point.

tagged: xml configuration code disco dependency injection container opinion

Link: https://blog.bitexpert.de/blog/why-using-code-as-di-config-is-a-win/

Symfony Blog:
Introducing Webpack Encore for Asset Management
Jun 13, 2017 @ 11:08:19

On the Symfony blog they've released an announcement about the release of a tool that wants to help make it easier for Symfony developers to work with frontend resources using the Webpack standard: Webpack Encore.

For everyone that has hit [the barrier of complexity in frontend dependencies and compilation], I'm very excited to show you something we've been working on for the last few months: Webpack Encore.

Encore gives you powerful CSS and JavaScript processing, combination, minification and a lot more, wrapped up in a simple API that's built on an industry-standard tool (Webpack).

He includes an example of the Javascript configuration to build out the Javascript, CSS and dependencies required for his build. He talks briefly about the conformity to the Webpack handling and how Encore fills that role in Symfony applications. The post ends linking to the project repository and the changes required to get the package installed.

tagged: webpack symfony encore library configuration frontend library dependency tool

Link: http://symfony.com/blog/introducing-webpack-encore-for-asset-management

Delicious Brains Blog:
Dependency Management and WordPress: A Proposal
Mar 23, 2017 @ 09:11:47

On the Delicious Blog Ian has written up a post with a proposal for WordPress suggesting that it introduce some functionality to help with dependency management and possible conflicts between the needs of plugins.

Dependency hell’ is a problem faced by all software, and it has been rearing its ugly head in the WordPress space over the last few years with more and more plugins using third-party libraries of code. [...] The most frustrating thing about this issue is that it’s caused by having the best of intentions! Developers use third-party code to be efficient and avoid reinventing the wheel. The code has been written by others and used and battled tested by many.

The WordPress community has a hard enough time already trying to get onboard with Composer (unlike the rest of the PHP world), without it getting tarred with the wrong brush!

He points out that, while this does have to do with packages installed through it, Composer itself isn't the issue. He offers a few suggestions and what he sees as an "ideal approach" to the problem based on some of the ideas presented here. He breaks it down into four types of code: third-party installed via Composer, Composer packages in core, custom Composer behavior and the idea of "package sandboxing". He includes some of the considerations to make this happen and plans on how the idea can move forward.

tagged: wordpress package dependency conflict proposal solution

Link: https://deliciousbrains.com/dependency-management-wordpress-proposal/

Using Dependency Injection in PHP
Mar 03, 2017 @ 10:05:37

The PHPBuilder.com site has posted a tutorial talking about dependency injection in PHP applications covering not only the use of dependency injection (DI) containers but also constructor, setter, property and reflection based injection methods.

Dependency injection is a software design pattern that implements the inversion of a control concept for resolving dependencies. According to this concept, a class should not configure its dependencies statically, but should be configured from the outside.

A dependency is an object that can be used (a service) and an injection is the passing of a dependency to a dependent object (a client) that would use it. Passing the service to the client, rather than allowing a client to build or find the service, is the fundamental requirement of the pattern.

The tutorial starts out with a simple example of a set of classes that depend on each other through the creation of internal objects. They then show how the different types of dependency injection can help reduce these dependencies with brief descriptions and sample code for each. The final example, a dependency injection container, gives a quick example but also links to several package options you could pull into your application including Pimple, Aura.Di and PHP-DI.

tagged: dependency injection tutorial introduction setter constructor container property

Link: http://www.phpbuilder.com/articles/application-architecture/design/using-dependency-injection-in-php.html

Master Zend Framework:
How To Generate Dependency Configuration's Easily with ConfigDumper
Jan 24, 2017 @ 13:45:26

The Master Zend Framework site has a new tutorial posted showing how to generate dependency configurations easily with the help of the ConfigDumper component is a Zend Framework based application.

Want to save time generating dependency configuration files for your Zend ServiceManager dependencies? In today's tutorial, I'll show you how, by using ConfigDumper, available in ServiceManager 3.2.0.

In the previous tutorial, we saw how to use FactoryCreator’s command-line tool, generate-factory-for-class, to quickly and easily create factories for classes.

In this, the follow-up tutorial, we’re going to see how to use generate-deps-for-config-factory, the command-line tool for ConfigDumper, to save time when generating dependency configuration files for use with our classes.

He starts by helping you get the correct version of the ServiceManager installed (3.2.0) and provides an overview of the generate-deps-for-config-factory tool. He moves on to a simple example using one of the included classes (the PingAction) and calls the generator with an example of the results. From there he includes a more complex example using the HomePageAction as its source. He points out that this tool doesn't work for every class and gives an example of a failure around a missing type hint. The post wraps up with a look at the ConfigAbstractFactory and how you can use the configurations that result from using the generation tool.

tagged: zendframework dependency configuration configdumper tool servicemanager tutorial

Link: http://www.masterzendframework.com/dependency-config-generation-with-configdumper/

Leonid Mamchenkov:
Dependency resolution with graphs in PHP
Nov 22, 2016 @ 10:52:23

Leonid Mamchenkov has a post to his site showing how he solved an interesting problem in one of his recent projects: determining the order to use items based on their dependencies.

One of the projects I am working on at work presented an interesting problem. I had a list of items with dependencies on one another and I needed to figure out the order in which to use those items, based on their dependencies.

He gives the example of database tables where it would be required to export the tables so that the relations between them are maintained when imported back in. He gives some example data, a basic nested PHP array, and defines the relationships between them (just strings in this case). While he did solve the problem, he wasn't happy with the solution. Instead he went looking for other options and found graph theory to be a good match. He briefly cover what the theory involves and links to an example that basically does what he needs...but is written in Python. He finishes off the post sharing his refactoring of this logic into PHP including a recursive "dependency resolver" and the output showing the correct ordering for loading objects based on their dependencies.

tagged: resolve dependency graph theory example tutorial load order

Link: http://mamchenkov.net/wordpress/2016/11/22/dependency-resolution-with-graphs-in-php/

Matt Allan:
Understanding Dependency Injection Containers
Jul 18, 2016 @ 11:54:54

In this recent post to his site Matt Allan introduces a concept that's become an integral part of most major PHP frameworks and applications recently: dependency injection containers.

If you are writing modern PHP, you will run across dependency injection a lot. Basically all dependency injection means is that if an object needs something, you pass it in. So if you have a class [...] you would pass in (inject) the object it needs (the dependency) instead of instantiating it in the class. Dependency injection makes your code more flexible and easier to test. If you want to learn more about dependency injection in general, check out this summary in the PHP The Right Way guide.

He then breaks down the main concept, the container, and how it is usually used to store instances of various objects and other functionality. He includes the code to create a simple container, allowing for closures to be set to "entries" values. He also shows how to update the simple container to allow for singleton handling, creating an object once and returning it over and over (useful in some cases).

tagged: dependency injection container tutorial introduction

Link: http://mattallan.org/2016/dependency-injection-containers/

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/

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