SitePoint PHPHow to Build Your Own Dependency Injection Container (12.2.2016, 17:00 UTC)

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.

Square wheel?

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. All of the code written in this article, plus PHPDoc annotations and unit tests with 100% coverage is available at this GitHub repository. It is also listed on Packagist.

Planning Our Dependency Injection Container

Let us start by planning what it is that we want our container to do. A good start is to split “Dependency Injection Container” into two roles, “Dependency Injection” and “Container”.

The two most common methods for accomplishing dependency injection is through constructor injection or setter injection. That is, passing class dependencies through constructor arguments or method calls. If our container is going to be able to instantiate and contain services, it needs to be able to do both of these.

To be a container, it has to be able to store and retrieve instances of services. This is quite a trivial task compared to creating the services, but it is still worth some consideration. The container-interop package provides a set of interfaces that containers can implement. The primary interface is the ContainerInterface that defines two methods, one for retrieving a service and one for testing if a service has been defined.

interface ContainerInterface
    public function get($id);
    public function has($id);

Learning From Other Dependency Injection Containers

The Symfony Dependency Injection Container allows us to define services in a variety of different ways. In YAML, the configuration for a container might look like this:

    # ...
    mailer.transport: sendmail

        class:     Mailer
        arguments: ["%mailer.transport%"]
        class:     NewsletterManager
            - [setMailer, ["@mailer"]]

Continue reading %How to Build Your Own Dependency Injection Container%

Stefan KoopmanschapThree Months Of Patreon (12.2.2016, 10:00 UTC)

Tomorrow it is three months ago that I launched my Patreon page. As I mentioned in my blogpost about launching my Patreon page, it was mostly an experiment to see how that would work out. I've seen a bunch of artists use Patreon very successfully, but the world of PHP, open source and software development, while it has some parallels, is different from the world of art, music and books. I was curious about how Patreon would be received in this world.

Since it was an experiment, any outcome would be a good outcome. I went without real expectations. Of course I was hoping it to be a major success, but with me being the only Patron of Rafael, I wasn't expecting much.

Now, three months later, I have two patrons. I am very grateful to Malte and Rafael for their support. I've promised to only charge them a maximum of once a month, and so far I've charged them twice. This does not mean I get a lot of money. With those two charges, I can now buy me a beer.

We are not used to paying for free content

I found out about Patreon through Amanda Palmer, who successfully uses Patreon to fund music she gives away for free or sells for a very low price. Most of her music is available for "name your price" (basically, $0 or more) on her bandcamp page. Her most recent release, the David Bowie tribute EP Strung Out In Heaven has a minimum of $1, but that's mostly to pay for the royalties she owes the Bowie estate. Yet for every Thing she releases, even though it is available for (nearly) free, she gets paid nearly $34000. I think that's awesome! And it makes sense, because this is her life. This is how she makes her money. From her music.

Through Lorna I found out about an article on A List Apart by Rachel Andrews on the high price of free. In that article, Rachel says some really important things. For instance:

As an industry we have become accustomed to getting hundreds of hours of work, and the benefit of years of hard-won knowledge for free.

This is what open source has done for us, and it is fantastic! If you run into a problem, you're a quick Google search away from finding the solution. If you come by a new piece of tech, most of the times there's already tons of documentation and blogposts on how to work with this tech. This is extremely valuable and one of the main reasons why open source is awesome: There is virtually no barrier of entry to get into programming, aside from the need for a computer and an Internet connection.

While this is awesome, it also means that we've grown accustomed to getting all this information for free. We gladly consume all this labour of love by passionate developers and content creators and expect things to be free. And of course we should, that is why the creators made it. What I do think, however, is that we should consider to show our appreciation a bit more.

We are used to paying for paid content

We consume free content without any problem, and we also consume paid content without any problem. Sure, there is a small problem of piracy for paid content, but mostly people will gladly pay for their php[architect] subscription, their copy of Chris Hartjes' books or one of the many awesome publication of Lorna Mitchell. We seem to be fine with paying for content that has a price on it. This is, obviously, a good thing. The people who create this content spend a lot of time on it and are, partially or fully, depending on the sales of their content for their income. I can recommend you a shitload of content that is very much worth the money (seriously, ask me if you need anything).

So about that free content

We're used to paying for content, that much is clear. So why not show your appreciation of the free content out there by paying for that as well? Patreon is only one way of doing so. Last December, I went into my Steam account and through my friends list, and randomly donated some games from the wantlist of those friends to them. Many content creators also have Amazon wishlists. Sometimes, people also have some paid product aside from their free work. For instance, Jordi has

Truncated by Planet PHP, read more at the original (another 1763 bytes)

Lorna MitchellPHP 7.0 (and 5.6) on Ubuntu (11.2.2016, 10:07 UTC)

PHP 7 is released but for those of us who don't usually compile our own PHP, it can be a long wait for our preferred distro to release the packages we want. For Ubuntu, I'm using a PPA which allows both PHP 5.6 and PHP 7.0 to be installed, including things like extensions, at the same time. It was very easy to set up (I'm running Ubuntu 15.10 but this process should also work on older versions back to at least 14.04 which is the previous LTS) so here's a quick walkthrough of what I did.

Add the PPA

The PHP 5.6 and PHP 7.0 packages are from a third party PPA, not provided by the official ubuntu repositories from Canonical. The PPAs I'm recommending here are from Ondřej Surý who packages PHP for Debian (which is then used by Ubuntu) so while it's not an official repo, he's not exactly random! The PPA itself is here:

Complete aside but an important one: The work of the packagers is voluntary and they enable the rest of us to do whatever it is that we do. If you want us to keep being able to have nice things, consider the donation page for this project, please?

To add the PPA to your setup:

sudo add-apt-repository ppa:ondrej/php

Then we'll also want to grab information about what is on offer from this new PPA, so then run:

sudo apt-get update

Install New PHP Versions

I already had some of the php5 packages installed, but I didn't uninstall anything, I just let apt work out what it wanted to do when I asked it to install the new versions:

sudo apt-get install php5.6 php7.0

This resulted in a lot of complaining from apt and lots of conflicts. The first suggested resolution was to remove all the stock php5 packages so that PHP 5.6 could be installed - so I just accepted the first suggestion.

I use apache so this setup gave me apache with both php5.6 and php7.0 modules available, and the php5.6 module actually loaded.

As well as just the PHP itself, all the extensions and other tools you'd expect with PHP are there for both versions of PHP so it's very easy to add in the modules that you need. I was very, very impressed with how nicely this is done.

Configuring and Switching Versions

Now you have two completely separate versions of PHP installed on your system, so let's have a look at where all the pieces went!

The config files are all in /etc/php/5.6 and /etc/php/7.0 respectively - inside here is where you can configure which extensions are loaded, set the ini settings, and everything else for each version in isolation.

I'm an apache user, and as I mentioned both modules are available. So to switch from one to the other I need to do:

sudo a2dismod php5.6
sudo a2enmod php7.0
sudo service apache2 restart

For nginx users, the changes are almost as easy, Digital Ocean have good documentation on this (they do have great docs!) so check out their guide: as it includes a section on reconfiguring nginx to use another version of PHP.

From the commandline, I have both php5.6 and php7.0 available as commands. I also still have a php command - look in /etc/alternatives to see that it symlinks to a particular version of PHP cli*. You can also quickly check which yours is using by running php -v.

* more specifically, run which php to see which version of PHP is being used - but this will probably point to /usr/bin/php, which for me is itself a symlink to the /etc/alternatives/php command.

Working with Extensions

This PPA comes with the usual php-pear package which offers the pecl command for both versions of PHP, so any extensions that are available via PECL can be installed in the usual way. You will also need the relevant headers so either php5.6-dev or php7.0-dev should be installed.

When the pecl installation completes, you'll get a note to add the *.so file to your php.ini; in fact the best thing to do here is to look at what's in /etc/php/mods-available. There will be some modules already here, each in its own file named after the extension and ending in .ini. You can copy one to use as a template or create your own and put all th

Truncated by Planet PHP, read more at the original (another 1248 bytes)

PHP ClassesPHP Tutorial to Increase Speed and Reduce Costs of AWS S3 using Caching (11.2.2016, 08:30 UTC)
By Joseluis Laso
Amazon S3 service is one of the most popular cloud storage services that you can use for many purposes like storing backups of important data or serve important files to many users.

Read this article to learn how to easily use Amazon AWS S3 API from PHP to store your files using caching to access the files faster and reducing the service costs.
SitePoint PHPBuilding an Spress Svbtle Theme – Responsive Static Blogs! (10.2.2016, 17:00 UTC)

You may have heard of Sculpin - a static site generator for PHP. Static blogs are, as you may know, blogs which you dynamically write in your app or on your local machine, and then export as a pure HTML version for hosting on static-only servers, for speed, reliability, and offline-first friendliness.

While easy to use and fast to set up, Sculpin’s development has stagnated a bit and the documentation leaves much to be desired. Spress is, in a way, its spritual successor. Much better documentation, much more flexible configuration, much easier to extend, and just as easy to use with almost the same API and commands.

Spress header image

In this tutorial, we’ll be building an Spress setup for generating a static blog with a custom theme.


This tutorial will assume you have a working PHP environment like Homestead Improved. For convenience, the following few lines will get you started immediately:

git clone hi_spress
cd hi_spress
vagrant up; vagrant ssh

After we’re in the VM, we can install Spress with:

sudo mv spress.phar /usr/local/bin/spress
sudo chmod +x /usr/local/bin/spress

Spress is now available system-wide (or VM-wide if you’re using a VM), which can be verified by running spress:

Screenshot of Spress output

To create a sample site, we can use the instructions from the docs:

cd ~/Code
spress site:new myblog spresso
cd myblog
spress site:build --server

The site should now be accessible via http://localhost:4000 or if you’re in a VM like Homestead Improved, you should first forward port 4000 in Homestead.yaml, run vagrant provision outside of the VM, and then access the site via

Screenshot of working demo app

Continue reading %Building an Spress Svbtle Theme – Responsive Static Blogs!%

Symfony CMFFirst Symfony CMF 1.3 Release Candidate Available (10.2.2016, 00:00 UTC)

The release of Symfony CMF 1.3 is coming closer with the availability of release candidates for all packages. Stable releases can be expected in the coming weeks.

After the 1.2 release, the activity of the Symfony CMF project has dropped, resulting in no new release in 2015. With this new release, the CMF is back in the running with full Symfony 2.8 and PHP 7 support.

Important new Features

Included in this release are the following packages:

  • Version 1.0 of SlugifierApi;
  • Version 1.1 of RoutingAutoBundle and RoutingAuto;
  • Version 1.2 of SearchBundle, SeoBundle and TreeBrowserBundle;
  • Version 1.3 of BlockBundle, CoreBundle, ContentBundle, MediaBundle, SimpleCmsBundle, Testing;
  • Version 1.4 of RoutingBundle and Routing;
  • Version 2.0 of MenuBundle;

The most important features included in these new versions are:

  • PHP 7 support for all CMF packages;
  • The Routing and Testing components support Symfony 3;
  • The CmfMenuBundle added KnpMenu 2 support and dropped support for KnpMenu 1;
  • The CmfSeoBundle added support for generating sitemaps and an exception controller for better error pages;
  • The CmfMediaBundle added a cmf_media_file form type to upload simple files;
  • Improved admin experience for most Sonata Admin classes, including integration of the SonataTranslationBundle for multi-language documents;
  • A symfony-cmf/slugifier-api package was introduced, containing a generic sluggifier interface to be used when a sluggifier is needed;
  • Integration of CKeditor in the CmfSimpleCmsBundle and CmfContentBundle, providing the admin with better tooling to edit page contents.

You can read about all new features master version of the documentation.

Please update your project and to test the release candidates. This will help making the 1.3 release more stable.

The Future

Symfony CMF 1.3 will be the last release in the 1.x series. After this release, work will start on Symfony CMF 2.0. This version targets Symfony 2.8 and 3.0+. In 1.3, most deprecated features already trigger deprecation notices. However, this does not mean the upgrade to 2.0 will not contain more breaking changes. We do our best to reduce the breaking changes as much as possible.

For 2.0, we will also try to reduce the number of bundles included in the CMF so that we can focus on the important parts. A first thing we already deprecated is the BlogBundle that never saw a stable release.

SitePoint PHPQuick Tip: Sync a Fork with the Original via GitHub’s Web UI (9.2.2016, 17:00 UTC)

I often find myself having to update my fork of someone else’s repo to include the changes made to the original since the fork. In fact, we use this approach often in SitePoint’s Peer Review System as well.

Purely by accident, I’ve recently discovered a way to quickly and easily sync a fork with the original repo through the Github web UI, so no need to go commando (use the command line).

Continue reading %Quick Tip: Sync a Fork with the Original via GitHub’s Web UI%

Voices of the ElePHPantInterview with Mathew Beane (9.2.2016, 05:00 UTC) Link
SitePoint PHPClean Code Architecture and Test Driven Development in PHP (8.2.2016, 18:00 UTC)

The Clean Code Architecture was introduced by Robert C. Martin on the 8light blog. The idea was to create an architecture which is independent of any external agency. Your business logic should not be coupled to a framework, a database, or to the web itself. With the independence, you have several advantages. For example, you have the ability to defer technical decisions to a later point during development (e.g. choosing a framework and choosing a database engine/provider). You can also easily switch the implementations or compare different implementations, but the biggest advantage is that your tests will run fast.

Just think about it. Do you really have to run through a router, load a database abstract layer or some ORM magic, or execute some other code just to assert one or more results?

I started to learn and practice this architecture because of my old favorite framework Kohana. At some point, the core developer stopped maintaining the code, which also meant that my projects would not get any further updates or security fixes. This meant that I had to either move to another framework and rewrite the entire project or trust the community development version.

Clean Code Architecture

I could have chosen to go with another framework. Maybe it would have been better to go with Symfony 1 or Zend 1, but by now that framework would have also changed.

Frameworks will continue to change and evolve. With composer, it is easy to install and replace packages, but it is also easy to abandon a package (composer even has the option to mark a package as abandoned), so it is easy to make “the wrong choice”.

In this tutorial, I will show you how we can implement the Clean Code Architecture in PHP, in order to be in control of our own logic, without being dependent on external providers, but while still using them. We will create a simple guestbook application.

Continue reading %Clean Code Architecture and Test Driven Development in PHP%

Thijs FerynBastian Hofmann – Talking about ResearchGate, Berlin & PHPBenelux (8.2.2016, 09:46 UTC)

Bastian Hofmann My friend Bastian Hofmann was flying to Brussels to speak at the PHPBenelux Conference that I organize. I

The post Bastian Hofmann – Talking about ResearchGate, Berlin & PHPBenelux appeared first on Thijs Feryn's blog.

LinksRSS 0.92   RDF 1.
Atom Feed   100% Popoon
PHP5 powered   PEAR
ButtonsPlanet PHP   Planet PHP
Planet PHP