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

Exakat Blog:
5 usages of static keyword in PHP
Jun 20, 2018 @ 10:28:21

On the Exakat blog there's a new post sharing five uses of the "static" keyword in PHP applications. This includes the less common static closures and static class names.

Static is a PHP keyword with many usages. It is almost universally used, though there are many variations of it. Let’s review all the five of them :
  • static method
  • static property
  • static closure
  • static variable
  • static as a classname

The post goes through each of the items in the list giving a brief explanation of how it's used and a code example showing it in action. The post finishes with some tips on evaluating your own code for the use of "static" and tips for each to make your code easier to wrangle and maintain in the future.

tagged: static keyword example tutorial method property closure variable classname

Link: https://www.exakat.io/5-usages-of-static-keyword-in-php/

Matthias Noback:
Road to dependency injection
Jun 12, 2018 @ 09:31:59

In a new post to his site Matthias Noback walks you down a road to dependency injection, sharing his process of migrating a codebase from static calls to a more modern, robust dependency injection pattern.

I've worked with several code bases that were littered with calls to Zend_Registry::get(), sfContext::getInstance(), etc. to fetch a dependency when needed. I'm a little afraid to mention façades here, but they also belong in this list. The point of this article is not to bash a certain framework (they are all lovely), but to show how to get rid of these "centralized dependency managers" when you need to.

He talks about this common use case of "statics everywhere" and some of the problems with coupling your code to a static way of doing things. He walks through the steps towards modernization including injecting the container itself into the controller and shifting logic out into services. He also makes suggestions on what to do when constructor injection isn't possible and how to deal with "temporal coupling".

tagged: dependencyinjection refactor codebase static inject container tutorial

Link: https://matthiasnoback.nl/2018/06/road-to-dependency-injection/

Matt Stauffer:
Adding an Auto-Generated Sitemap to Your Jigsaw-based Static Site
May 29, 2018 @ 11:07:42

If you're a user of the Jigsaw static site generation tool from Tighten, Matt Stauffer has a new post to his site that could come in handy. He shows how to automatically create a sitemap.

There are a few tasks that are still pretty tough with static sites—for example, search, and submitting forms (which we're trying to fix with FieldGoal). But there are other tasks that are tough-but-possible, and key among them are RSS and sitemaps. Let's start with sitemaps.

There was a feature recently added to help with this allowing you to add extra "hooks" to fire when the site builds. Matt makes use of this (and this package) to create a GenerateSitemap class that goes through each item in the site and adds a link to the sitemap.xml file. He shares the code for this class and the updates to make to the configuration and bootstrapping to correctly load the class.

tagged: generated sitemap jigsaw static site package tutorial

Link: https://mattstauffer.com/blog/adding-an-auto-generated-sitemap-to-your-jigsaw-based-static-site/

Tomas Vortuba:
How I Got into Static Trap and Made Fool of Myself
Apr 30, 2018 @ 10:38:38

Tomas Vortuba has a new post to his site sharing some of his experiences with static function analysis (and fixing) and the trap he got himself into when evaluating the current state of his code. This is a continuation of his series covering the use of the SymfonyCodingStandard package.

PHP story with code examples, copy-cat killers, just a little bit of static, consistency, sniffs and way to prevent all that ever happening ever again.

In his post he uses an example "fixer" for testing DocBlock comments to see if they provide much useful information over what the PHP itself provides. He dives "under the surface" and looks at the steps it takes to accomplish this. He then gets to the "trap" part, showing the code behind the fixer and how depending on 3rd party packages and their parsing lead to code mistakenly being "fixed". He finishes up the post with a look at a few solutions that could help make these "traps" easier to fix including dependency injection and preventing the issue specifically with static functions.

tagged: static function analysis fixer codingstandard tutorial

Link: https://www.tomasvotruba.cz/blog/2018/04/26/how-i-got-into-static-trap-and-made-fool-of-myself/

Laravel News:
Going Deeper with Factories Through Factory States
Apr 11, 2018 @ 10:11:58

The Laravel News site has posted a tutorial that takes you deeper with factories when using the framework looking at the factory states.

I suspect that if you are familiar with Laravel that you might use factories and possibly even factory states in your application development. The documentation shows you the mechanics of using factories to seed and create test data, but there are a couple of guiding thoughts to using factories with your models effectively that I’d like to consider.

He starts with two options he sees for using factory states: making them with static values or using them to make simple models instead. He goes through these two options, introducing some of the basic concepts of each, how it would work and the code to make it happen. He also covers a few other approaches including the use of a trait to include the factory functionality directly in a class. He finishes the post with a few links to more information in the Laravel manual and other outside resources/tools.

tagged: laravel factory state static value model tutorial

Link: https://laravel-news.com/going-deeper-with-factories-through-factory-states

Mark Baker:
Discharging Static #2
Apr 05, 2018 @ 10:22:09

Mark Baker continues his series looking at the use of static properties and methods in applications and how to test them. In this second part of the series he focuses more on some of the unintentional side-effects that could happen when you're trying to refactor them out.

In the first article in this series, I wrote about the problems with testing static methods in classes, and showed a few approaches that allow us to write mocks for statics. Testing classes where we have static properties is a lot harder to manage, because any changes made in any test to those properties are global, across the entire test suite, not simply the test that we are running. Static properties are part of the global state, and we can’t simply tearDown() those changes in the way that we do with instances — at least we cannot easily do so if the property has a visibility of private or protected.

He goes through an example of a refactor from a static property (essentially in the global scope) to a private property. He points out the issue of setting a static value in what seem to be separate child classes, the fact that it actually changes the base value, not the individual ones, leading to potentially unintended consequences.

His main recommendation is to avoid the use of static properties all together. Where that's no possible (like in a legacy project) he offers two potential solutions: either replace them with constants if they're never changed or changing them to instance properties.

tagged: static property series part2 refactor consequences testing

Link: https://markbakeruk.net/2018/04/03/discharging-static-2/

Marc Baker:
Discharging Static #1
Mar 14, 2018 @ 14:49:47

On his blog Marc Baker about static calls and the trouble they bring in your applications. It's a continuation of ideas that Kore Nordmann shared previously on his site.

It’s been seven years since Kore Nordmann first published “static considered harmful” on his blog, explaining the problems with using static method calls in classes, and the difficulties that they cause when trying to test that class. Seven years on, and those difficulties are still the same, and there is still new code being written using static calls despite that knowledge; but it’s often a more severe problem in legacy code with little or no unit tests.a

So why exactly are static calls so bad? If you’ve read Kore’s article, then you probably have a good idea already; but what that article doesn’t cover is approaches that we can use to make the code testable.

He covers the main issue static methods have when it comes to testing: they introduce coupling by hard-coding a dependency into your code. He talks about the static testing functionality that various PHP testing tools provided: PHPUnit, Phake and Mockery. He then focuses on a newer tool that he's discovered to help make the testing simpler: AspectMock. He gives an example of it in use and some examples of tests using anonymous classes to make it easier to create tests on the fly.

tagged: static testing phpunit example harmful tutorial aspectmock

Link: https://markbakeruk.net/2018/03/13/discharging-static-1/

Tim MacDonald:
Backup multiple sites and frameworks with 'Laravel Backup'
Nov 01, 2017 @ 10:56:19

In a new post to his site Tim MacDonald covers a Laravel backup package provided from Spatie and how to use it to backup your sites (and not just Laravel ones).

Using Spatie's Laravel Backup package you can backup several sites on a server from a single app install, including Laravel apps and WordPress sites.

Laravel Backup is a fantastic package brought to us by to ever awesome team at Spatie, and is only one of the many great packages they have in the wild. This package makes it super simple to backup your Laravel applications, including all your files and production database, to a 3rd party storage facility, such as Amazon S3.

He then describes his own setup, running a smaller server that hosts several of his sites that use Laravel, WordPress and static content. He needed a backup solution that would allow for him to manage backups for all of them. He then walks through the setup of the package and how to configure it for each site type. The post finishes with the command to execute and run the backup for the site and how to schedule it as a part of the recurring jobs handling.

tagged: laravel backup tutorial introduction setup configure wordpress static

Link: https://timacdonald.me/backup-multiple-sites-frameworks-laravel-backup/

Hayden James:
PHP-FPM tuning: Using ‘pm static’ for max performance
Oct 13, 2017 @ 11:28:13

Hayden James has a post on his site sharing a method he's found for getting better performance out of PHP-FPM with the help of a configuration change. In this tutorial he shows how to use the pm static setting to squeeze the best performance out of your web server.

Lets take a very quick look at how best to setup PHP-FPM for high throughput, low latency and a more stable use of CPU and memory. By default, most setups have PHP-FPM’s PM (process manager) string set to dynamic and there’s also the common advice to use ondemand if you suffer from available memory issues.

He starts by defining the three "pm" settings and what they do: dynamic, ondemand and static. He then talks some about how the PHP-FPM process manager is similar to CPUFreq Governor and the settings it allows. Finally he gets into talking about the "pm static" handling, how it relates to available memory and when it makes more sense to use "dynamic" over "static".

tagged: phpfpm tuning performance static dynamic ondemand tutorial memory

Link: https://haydenjames.io/php-fpm-tuning-using-pm-static-max-performance/

Master Zend Framework:
How to Create a Zend Expressive Module
Jul 26, 2017 @ 11:14:52

On the Master Zend Framework site Matthew Setter has written up a tutorial showing you how to create a Zend Expressive module to help organize your application and keep related files all in one place.

I've been reflecting recently on the things that I commonly have to do when I begin building Zend Expressive applications. Of the list that I created, I found that one of the most common — and uninteresting — of them is setting up the rendering of static page content. [...] To solve this, I've usually manually created modules to handle the rendering of static page content. These module's usually had little more than a PageAction class that returned an HtmlResponse object, whose body was a rendered template file.

[...] If you're in a similar position to where I was and want to collate the logic into a reusable module, then follow along with me in this tutorial I'm going to step you through how to create a module which you can reuse across any Zend Expressive project.

He starts by briefly describing a "module" in the context of a Zend Expressive application and how they relate (or don't) to the modules in Zend Framework v2. He then starts in on the module creation, showing how to set up the environment, change the Composer configuration to autoload correctly and describes what the end result should look like. From there the tutorial starts on the code required for the classes covering the basics of the class, exception handling, configuration and the creation of a factory to make a module instance.

tagged: zendexpressive tutorial module static content zendframework

Link: http://www.masterzendframework.com/create-a-zend-expressive-module/