Destructuring arrays in PHP: Practical examples

Being more focussed on JavaScript nowadays, I kinda forgot that it’s possible to destructure arrays in PHP ever since the release of PHP 7.1. Frank de Jonge provides us with some practical examples such as this simple one: // JavaScript let options = {enabled: true, compression: ‘gzip’}; let { enabled, compression } = options; console.log(enabled); …

IoC containers beyond constructor injection

Talk by Hannes Van De Vreken, as given at the recent phpCE conference in Poland: Did you know your IoC container can do a whole lot more than just constructor injection? Besides that it is actually packed with features. Inflectors, resolving callbacks, aliasing, method invocation to name a few. In this talk you will learn …

Easily set Content Security Policy headers in Laravel with laravel-csp

Speaking of Content Security Policy, the folks at Spatie – who else? – have created a Laravel Package to easily take care or your CSP needs in a Laravel-based app. Even without knowing the inner workings of the packge, the custom Policy below is easy to understand: namespace App\Services\Csp; use Spatie\Csp\Directive; use Spatie\Csp\Policies\Policy as BasePolicy; …

PHP Session Locking: How to Prevent Blocking Requests

Today I learned about “PHP Session Locking”: PHP writes its session data to a file by default. When a request is made to a PHP script that starts the session (session_start()), this session file is locked. What this means is that if your web page makes numerous requests to PHP scripts, for instance, for loading …

Browsershot: Convert a webpage to an image or PDF using headless Chrome

The folks over at Spatie have just release Browsershot v3: Browsershot is a PHP package which can convert a webpage to an image or pdf. The conversion is done behind the scenes by Puppeteer which controls a headless version of Google Chrome. Conversion is easy-peasy, hence this example: use Spatie\Browsershot\Browsershot; // an image will be …

Macroable – A Trait to Dynamically add Methods to a PHP Class

New package by the folks from Spatie: We recently released our newest package called macroable. It contains a trait that, when applied to class, can dynamically add methods to that class. This trait is basically a stand alone version of the macroable trait in Laravel. It works by leveraging the __call() magic function, and checking …

Getting Ready for PHP 7.2

PHP 7.2 is planned to be released on 30th November 2017. And it comes with two new security features in the core, several smaller improvements and some language legacy clean-ups. In the article, I will describe what the improvements and changes are. I read the RFCs, discussions on internals and PRs on Github, so you …

Silex Routing vs Trailing Slashes

In Silex-based projects I always define my routes with a trailing /. When defining a route with a trailing slash, the router will respond to both the route without and with slash: $app->get(‘/hello’, …); will respond to http://localhost/hello but not to http://localhost/hello/ $app->get(‘/hello/’, …); will respond to http://localhost/hello and to http://localhost/hello/ Unfortunately this only works …

Automatically rerun PHPUnit tests when source code changes with phpunit-watcher

Nice new package by Freek from Spatie.be. Think of it like Jest, but for PHP: Wouldn’t it be great if your PHPUnit tests would be automatically rerun whenever you change some code? This package can do exactly that. By default it will watch all files in the src, app and tests subdirectories in the directory …

Zttp, a developer friendly wrapper for Guzzle

If you’re not familiar with the evolution of Guzzle, the library has basically gotten more professional and less usable with each new version. New layers upon layers of specification-respecting abstractions and rules made Guzzle incredibly difficult to get started with. Zttp solves just that, by keeping things simple: Zttp is a simple Guzzle wrapper designed …