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

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

Tighten.co:
Introducing Jigsaw, a Static Site Generator for Laravel Developers
Apr 20, 2016 @ 13:33:40

On the Tighten.co blog there's a new post introducing Jigsaw, a static site generator for Laravel developers they've created in the course of their own work.

That's right, Tighten has created a Laravel-based static site generator named Jigsaw, and we think it's pretty great.

Before I write another line of this post, I want to address the looming question: Why create another static site generator? In PHP alone there are two, and since we soft-launched Jigsaw there's already been another Blade-based static site generator launched.

The first part of the article lists three reasons for making the tool, pointing out the ecosystem they used (different from others), the focus on Laravel developers and the easy transition from a Jigsaw site to a full Laravel one. From there the post talks about what Jigsaw is and how you can get started using it (installation and configuration guide included). It also includes examples of "first pages" to help you get started and the result. The post finishes with a look at variable handling, custom front matter values, deployment and how to convert the site from Jigsaw to Laravel should the need arise.

tagged: jigsaw static site generator laravel introduction installation tutorial

Link: http://blog.tighten.co/introducing-jigsaw-a-static-site-generator-for-laravel-developers

Matthew Weier O'Phinney:
Serve PSR-7 Middleware Via React
Apr 20, 2016 @ 12:07:56

Matthew Weier O'Phinney has a post to his site showing you how to combine PSR-7 request/response handling (his examples use Zend Expressive) with React and middleawre in your application.

I've been intending to play with React for some time, but, for one reason or another, kept putting it off. This past week, I carved some time finally to experiment with it, and, specifically, to determine if serving PSR-7 middleware was possible.

He starts with a brief introduction to React and what kind of functionality it brings to the table. He includes a bit of sample code showing it in use creating a basic HTTP server responding to any request with a simple "Hello World" message. He then starts on the React+PSR-7 integration, wrapping the request and response handling from one in the other to keep the expected responses the same. He also talks about serving up static files using the React+PSR-7 handling via a middleware on the Expressive side. Finally he shares the work he's done via a library to help make it easier to reuse in other situations. He shows the installation and usage of this library and sample requests you can use to test it out.

tagged: react psr7 request response example library handler static file tutorial

Link: https://mwop.net/blog/2016-04-17-react2psr7.html

Nginx.com:
Maximizing PHP 7 Performance with NGINX, Part I: Web Serving and Caching
Feb 29, 2016 @ 13:55:10

On the Nginx.com site they've posted the first part of a series showing you how to maximize your performance with PHP 7 and this already speedy web server.

PHP is the most popular way to create a server-side Web application, with roughly 80% market share. (ASP.net is a distant second, and Java an even more distant third.) [...] Now the PHP team is releasing a new version, PHP 7 – more than a decade after the introduction of PHP 5. During this time, usage of the web and the demands on websites have both increased exponentially.

[...] This blog post is the first in a two-part series about maximizing the performance of your websites that use PHP 7. Here we focus on upgrading to PHP 7, implementing open source NGINX or NGINX Plus as your web server software, rewriting URLs (necessary for requests to be handled properly), caching static files, and caching dynamic files (also called application caching or microcaching).

They start by looking at why "PHP hits a wall" in its execution in high load situations, stepping through the process it follows to handle each request. They also share some of the common ways PHP developers have combatted these issues including more hardware, better server software and multi-server setups. They then get into the actual tips themselves:

  • Tip 1. Upgrade to PHP 7
  • Tip 2. Choose Open Source NGINX or NGINX Plus
  • Tip 3. Convert Apache Configuration to NGINX Syntax
  • Tip 4. Implement Static File Caching
  • Tip 5. Implement Microcaching

For each tip there's a summary with more information on why they make the suggestion and, for some, how to make the transition happen. In the next part of the series they'll get into reverse proxy servers and a multi-server Nginx implementation to boost performance even more.

tagged: performance php7 nginx series part1 maximize tutorial static cache apache conversion

Link: https://www.nginx.com/blog/maximizing-php-7-performance-with-nginx-part-i-web-serving-and-caching/

SitePoint PHP Blog:
Building an Spress Svbtle Theme – Responsive Static Blogs!
Feb 11, 2016 @ 12:47:11

On the SitePoint blog there's a tutorial posted showing you how to create a responsive site template in Spress, a static site generator written in PHP.

You may have heard of Sculpin – a static site generator for PHP. [...] 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 spiritual 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.

He starts by helping you set up a basic site to work with on a Homestead Improved instance. Once that's up and running (including an install of Spress) he creates the simple site and starts in on the rebuild of the Svbtle theme. He briefly explains how Spress themes work and then includes the code/layouts you'll need to reproduce the theme. The post includes a screenshot of what the end result should look like in two different browser sizes (responsive, remember).

tagged: spress static site generator responsive theme svbtle tutorial

Link: http://www.sitepoint.com/building-an-spress-svbtle-theme-responsive-static-blogs/

Anna Filina:
Testing Methods That Make Static Calls
Jan 13, 2016 @ 09:03:40

Anna Filina has posted a quick hint around testing methods that make static methods calls to other parts of your application. Static method calls are notoriously difficult to test, especially with PHPUnit.

I had trouble testing a particularly painful codebase. It had static calls and implicit dependencies all over the place, to name just a few problems.

One of the things that it often did was to call static methods that would increment counters in the database and cache stuff. Example: Record::incrementViews() It was making things difficult. To avoid messing with the original codebase too much, I came up with this quick and dirty way to ignore those dependencies.

Her solution makes use of a mockStaticDependency method that then turns around and redefines the class in question (like her "Record" above) with a __callStatic through an eval. She points out that usually using eval is "evil" but in this case it made testing the functionality much simpler when no feedback was needed from the static method. In the comments on the post, someone also makes a recommendation of the Patchwork library for PHP that allows for "monkey patching" and modifying classes/functionality to redefine functions and methods in a similar way.

tagged: unittest method static call monkeypatch eval callstatic example

Link: http://afilina.com/testing-methods-that-make-static-calls/

Rob Allen:
Running Phan against Slim 3
Dec 10, 2015 @ 09:51:20

Rob Allen has a quick post sharing the results of a test run of the Phan static analysis tool on the current state of the Slim 3 framework codebase (with v3.0 just being released).

Having installed Phan, I decided to use it against the upcoming Slim 3 codebase.

Phan needs a list of files to scan, and the place I started was with Lorna's article on Generating a file list for Phan.

He walks through the steps for creating this list of files (removing developer dependencies) and the results from the Phan execution. While a good amount of the errors related more to dependencies and missing class/interface definitions, there were some typing errors found based on the difference between the docblock and how the code handled the variable.

tagged: phan static analysis tool slim3 framework results

Link: https://akrabat.com/running-phan-against-slim-3

Rob Allen:
Installing Phan on OS X
Dec 03, 2015 @ 09:27:37

Rob Allen has posted a quick tip to his site showing how to get Phan installed on an OS X system. Phan is a static analysis tool written for PHP 7 and makes use of the new functionality that exposes the AST for the underlying language.

I use Homebrew for my local PHP installation on OS X and am currently running PHP 7.0.0 RC8.

Phan is a static analyser for PHP 7 which was written by Rasmus and then rewritten by Andrew Morrison. As it benefits from PHP 7's abstract syntax tree it can find all kinds of subtle errors, so I wanted to install it locally to have a play with it.

He shows how to get the tool installed via Composer (with a custom repository definition) and links to the ast extension you'll need installed to let the tool work. A quick exit to your php.ini file is then all it takes to complete the installation and let you install and run the tool from the command line.

tagged: phan static analysis tool php7 install configure osx

Link: http://akrabat.com/installing-phan-on-os-x/

Lorna Mitchell:
Generating a File List for Phan
Nov 27, 2015 @ 10:38:33

Lorna Mitchell has shared a tip she's found helpful when using the phan static analysis tool for finding only PHP files via a simple grep.

Phan is the PHP Analyzer for PHP 7 code. I've been using it, partly out of curiosity, and partly to look at what the implications of upgrading my various projects will be. [...] I generated my filelist.txt files with a little help from grep - by looking for all files with opening PHP tags in, and putting that list of filenames into a file.

The phan tool is still pretty young but it provides a good example of how to use the new php-ast handling to parse and analyze PHP code.

tagged: phan file list generate quick tip grep static analysis tool

Link: http://www.lornajane.net/posts/2015/generating-a-file-list-for-phan

Ross Tuck:
Formatting Exception Messages
Oct 27, 2015 @ 12:09:39

In a post to his site Ross Tuck shares some of his experience and some helpful hints around formatting exception messages and how doing so effectively can make life for fellow developers much easier.

Over the last couple years, I’ve started putting my Exception messages inside static methods on custom exception classes. This is hardly a new trick, Doctrine’s been doing it for the better part of a decade. Still, many folks are surprised by it, so this article explains the how and why.

He shares his tips as a part of a "refactoring" in a simple example, a CSV import where there are failures during the import process on certain lines. He starts with the basic Exception and works through the logic to customize it and make it more useful. He shows the inclusion of additional details in the message, abstracting out the formatting to custom methods based on the error type and using static methods for the more complex message formatting. He also suggests the creation of methods to handle specific error cases with more details than a simple single-line error in a normal exception being thrown.

When you co-locate the messages inside the exception, however, you gain an overview of the error cases. If these cases multiply too fast or diverge significantly, it’s a strong smell to split the exception class and create a better API. [...] Sometimes we underestimate the little things that shape our code. [...] Creating good environments at a high level starts with encouraging them at the lowest levels. Pay attention to what your habits encourage you to do.
tagged: format exception message custom method details static

Link: http://rosstuck.com/formatting-exception-messages/