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

Shameer C:
Slim 3: Replacing Pimple with Aura.Di
Sep 28, 2015 @ 12:12:25

In a post to his site today Shameer C shows you how to replace Pimple in Slim 3 as an application's dependency injection container. He replaces it with the Aura.Di container, a relatively easy replacement thanks to the container interoperability efforts.

Slim framework is my go-to micro framework for all small projects because of it's simplicity and easiness to use. The new version of this cool framework is just around the corner and they have released RC-1 a few days back. [...] Slim 3 has a default DI Container that extends Pimple. [...] Though Slim uses Pimple by default, we can replace it with any DI container that implements Container Interoperability interface, which is a really great feature. The only caveat is Slim sets some of it's required services from the default container. So we should do it by ourself when we use a different library.

He starts by talking a bit about the Aura.Di container and how it's different than what Pimple has to offer. He then links to a small library that makes the bridge between Slim 3 and Aura.Di and sets up the services Slim needs to operate correctly. He then steps through the code needed to integrate this into a simple Slim application based on this skeleton. Most of the work is done in the dependencies.php file where Slim's needs are set up, configured and injected into the container.

tagged: auradi dependency injection container slim3 pimple replace tutorial

Link: http://blog.shameerc.com/2015/09/slim-3-replacing-pimple-with-auradi

Rob Allen:
Accessing services in Slim 3
Jun 23, 2015 @ 10:51:36

Rob Allen has a new post to his site today showing you how to access services in a Slim 3 application using container injection instead of the previous "getInstance" method.

One of the changes between Slim Framework 2 and 3 is that the application singleton has gone. [...] In general, you didn't need access to $app itself, but rather you wanted access to something that the app knows about, such as a database adapter, or the router for access to the urlFor method to create a URL to a route. With Slim 3, there is no getInstance() on App, so you need to inject the instances of whatever you need where ever you need them.

He shows you how to create a simple Slim dependency injection container (service locator?) and push two kinds of objects in for later reuse. He shows how to reference this container from inside of your routes in both the callable/closure and class contexts. He also includes an example of referencing the same container from inside middleware (again in both the closure and class contexts).

tagged: slim microframework framework slim3 service access container this

Link: http://akrabat.com/accessing-services-in-slim-3/

New Media Campaigns:
Docker for PHP Developers
Jun 02, 2015 @ 10:29:38

The New Media Campaigns site has posted a new tutorial today introducing PHP developers to Docker, the handy tool to create containers for your applications a bit simpler and more efficient than just something like Vagrant.

I've used Vagrant to manage local development servers for several years. Vagrant is, according to its official website, a tool to "create and configure light-weight, reproducible, and portable development environments." [...] However, Vagrant has one large downside—it implies hardware virtualization. This means each project runs atop a full virtual machine, and each virtual machine has a complete operating system that demands a large overhead in system resources.

[...] There is another solution, though. Have you heard of Docker? I first heard this word a year ago. It's all about containers, I was told. Awesome. What are containers?, I thought. I dug deeper, and I read all about containerization, process isolation, and union filesystems.

He starts with a brief introduction to what Docker is and two of the key concepts: containers and images. He then talks about how Docker is different from Vagrant, including the extensibility and lighter resource demands. Following all this he starts in on building an actual application in a container. He walks you through each step, including commands, to build the container and image that will result in the final instance running Ubuntu, MySQL, Nginx and PHP-FPM. He sets up a simple "Hello World" page and shows how to configure the Nginx server to serve it up as well as the MySQL server to cooperate with PHP and run locally.

tagged: docker introduction container image configure server setup tutorial

Link: http://www.newmediacampaigns.com/blog/docker-for-php-developers

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).

tagged: dependency injection di container tutorial introduction

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

Cody Kennedy-Darby:
Testing Your Current Code Against PHP7 Or HHVM
Apr 01, 2015 @ 08:49:11

On Medium.com Cory Kennedy-Darby has a quick post showing you how you can test your current code against the latest versions of PHP 7 with the help of DUnit.

DUnit (dee-unit) makes your life easier by allowing you to run your unit tests on different versions of PHP or HHVM. Different versions are possible by using Docker containers. Thanks to @danbruce each of the Docker containers are only, ~35 MB in size. [...] PHP7 isn’t that far away. In fact, it is scheduled for release in ~8 months in November. Now is the perfect time to start testing your code against PHP7 nightly.

He starts with a few reasons you might want to test your code and things you can do to start "thinking forward" to when it is released. He then shows you how to install DUnit (more detail here) and use it to test on both PHP 7 and HHVM builds.

tagged: nightly test unittest build dunit docker container php7 hhvm

Link: https://medium.com/@ckdarby/testing-your-current-code-against-php7-or-hhvm-2f0ab059af78

Laravel News:
5 Resources to Learn about the Laravel IoC Container
Jan 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.

tagged: laravel inversion control ioc container dependency injection tutorials

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

Remi Collet:
PHP-FPM in Docker
Dec 12, 2014 @ 11:57:35

Remi Collet has a new post today showing you how to get PHP-FPM up and running with Docker using a few simple lines in the Dockerfile. Docker is a toolset that lets you easily create and provision containers with scripted configurations (and link them together).

[The] use case [for this is] running php 5.3.3 on a Fedora 20 / 21 development workstation, for production deployment on RHEL-6 (as no php 5.3 SCL exists). This example can be easily adapted for all available PHP versions available as RPM (5.3.3 in RHEL-6, 5.4.16 in RHEL-7, 5.4.16 and 5.5.6 in RHSCL 1.2 or using a third party repository).

The contents of the Dockerfile are included, making a call to yum to install all the needed packages, make a few replacements in the www.conf configuration file and create the default "www" directory. Finally, it fires up the PHP-FPM server with the IP given in the startup. The commands to create the container and launch it are also included in the post.

tagged: tutorial phpfpm docker container create configure

Link: http://blog.famillecollet.com/post/2014/12/11/PHP-FPM-in-Docker

Digging in to Laravel's IoC Container
Nov 25, 2014 @ 12:23:07

NetTuts.com has a new tutorial posted that digs into the Laravel IoC (Inversion of Control) container, one of the key features of the framework making it easy to create and use objects all around your applications.

Inversion of Control, or IoC, is a technique that allows control to be inverted when compared to classical procedural code. The most prominent form of IoC is, of course, Dependency Injection, or DI. Laravel's IoC container is one of the most used Laravel features, yet is probably the least understood.

He starts with an example of basic dependency injection (constructor injection) and how this relates to the Laravel framework's IoC handling (hint: it's all IoC). He includes examples of some built-in Laravel bindings and talks about the difference between shared and non-shared bindings. He also looks at conditional binding, how dependencies are resolved and how you can define your own custom binding implementations. Other topics mentioned include tagging, rebounds, rebinding and extending. He ends the article with a look at how you can use the IoC outside of Laravel too.

tagged: laravel ioc container inversionofcontrol framework tutorial introduction detail

Link: http://code.tutsplus.com/tutorials/digging-in-to-laravels-ioc-container--cms-22167

Gonzalo Ayuso:
Building a Pimple/Container from a YAML file
Sep 29, 2014 @ 10:54:10

In a recent post to his site Gonzalo Ayuso shows how to create a Pimple container (a dependency injection container) from a YAML configuration definition using a simple handler already included in Pimple.

[In a conversation with Máximo Cuadros about Dependency Injection Containers] I said that I prefer Symfony´s DIC instead of Pimple, mainly because its configuration with YAML (or even xml) files. But In fact we can customise Pimple/Containers with YAML files in a similar way than we do it with Symfony’s DIC. In this example we’re going to see one way to do it.

While Pimple does come with the "Container" functionality to make this happen, Gonzalo points out that using it this way violates several of the SOLID design principles. Instead, he offers an alternate solution - using the Config component from Symfony to handle the creation of the container using an already established YAML format. He includes an example YAML configuration structure, the matching Pimple code for it and a code sample showing how the YAML is parsed into the same result. You can find the code on his GitHub account if you're interested in the full structure.

tagged: pimple symfony container solid config yaml dependencyinjection

Link: http://gonzalo123.com/2014/09/29/building-a-pimplecontainer-from-a-yaml-file/

SitePoint PHP Blog:
PHP Dependency Injection Container Performance Benchmarks
Aug 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.

tagged: dependency injection benchmark performance container

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