Named arguments in PHP 8

Thu 23, Jul 2020


One of the coolest new features of PHP 8 are named arguments (parameters). I'm really happy that they made it into PHP 8, as they're a huge improvement syntax-wise, I think. But first, take a look at what they look like:

class Post {

    public function __construct(
        string $title,
        string $body,
        string $author
    ) {}


# ...

$post = new Post(
    title: 'Some interesting post',
    body: 'Bla bla body text here',
    author: 'John'


While this feature was highly debated, I think it comes with far more advantages than disadvantages. It allows for cleaner and easier to understand code (think (min: 0, max: 86400) instead of (0, 86400)). It also allows us to skip default values. Say we have the following function:

function cacheText(
    string $key,
    string $value,
    int $expires = 86400,
    string $directory = 'cache/'
) {}

If we now wanted to call this function with a different directory, but keep $expires with its default value, we can now write this

    key: 'content-home',
    value: 'something bla bla',
    directory: 'someOtherDir/'

instead of that:

    'something bla bla',

Named arguments can also be used together with ordered arguments. That means that you could, for example, write this:

$post = new Post(
    'Some interesting post',
    body: 'Bla bla body text here',
    author: 'John'

What would not work, however, is doing it this way:

$post = new Post(
    title: 'Some interesting post',
    'Bla bla body text here',
    author: 'John'

You can only use ordered arguments together with named ones at the beginning of the argument list. If you're using ordered arguments between named ones, PHP throws an error.

Named arguments are also supported in annotations/attributes, by the way!

In case you want some more info on that, view this article and the RFC!

PHP 8 Attribute Syntax

Thu 02, Jul 2020

So @@ reaches the quota and has been elected as the final attribute syntax for PHP 8.

@@ will be the attribute syntax for PHP 8:

class Something {}

I personally think that choosing @@ was the right decision.

If you don't really know what's going on with those attributes, read this!


New in PHP: str_contains

Mon 16, Mar 2020

RFC str_contains has been merged into PHP:

str_contains checks if a string is contained in another string and returns a boolean value (true/false) whether or not the string was found.

It's basically just a shorter way of writing strpos($haystack, $needle) !== false. It will be available in PHP 8.

Example (taken from the RFC page):

str_contains('abc', 'a'); // true
str_contains('abc', 'd'); // false

// $needle is an empty string
str_contains('abc', '');  // true
str_contains('', '');     // true

JSON responses, redirects and cookies in Sierra

Mon 09, Mar 2020

Please note that this article refers to a very old version of Sierra. This does not work anymore with newer versions of it!

Just wanted to show you some cool new features in Sierra:

  1. JSON responses
  2. Redirect responses
  3. Cookies

JSON responses

Just create a new Sierra\HTTP\JSONResponse and pass it some data:

use Sierra\MVC\Controller;
use Sierra\HTTP\JSONResponse;

class Home extends Controller {

    public function index() {
        new JSONResponse(array(
            'hello' => 'world'


Redirect responses

Redirecting a user has never been easier:

use Sierra\MVC\Controller;
use Sierra\HTTP\RedirectResponse;

class Home extends Controller {

    public function index() {
        # Will redirect the user to /new/target/
        new RedirectResponse('/new/target/');


Constructing this class will also print some HTML containing a .


Cookies can now be set using $this->res->set_cookie() in controllers:

use Sierra\MVC\Controller;
use Sierra\HTTP\Cookie;

class Home extends Controller {

    public function index() {
        $this->res->set_cookie(new Cookie('name', 'value'));
        echo 'Cookie has been set!';


Releasing my PHP framework

Fri 06, Mar 2020


I've just released my PHP MVC framework on GitHub. It's a rewrite with a lot of newly added extensions of the framework I'm using on this and many other sites.

You can check it out at sierra-project/sierra. Feedback is highly appreciated! :)

Lazy loading images in Parsedown

Thu 13, Feb 2020

Parsedown is probably the most used Markdown engine for PHP (it also powers this site!). I wanted to integrate loading="lazy" into Parsedown, as it's now part of the HTML standard.

This is my approach:

class ParsedownLL extends Parsedown {

    protected function inlineImage($excerpt) {
        $image = parent::inlineImage($excerpt);

        if (!isset($image)) {
            return null;

        $image['element']['attributes']['loading'] = 'lazy';

        return $image;


Keep in mind to use ParsedownLL instead of Parsedown, but include both.

How to install PHP 7.4 on Debian

Fri 20, Dec 2019

Installing PHP 7.4 on Debian is very simple:

apt-get install apt-transport-https lsb-release ca-certificates curl -y
wget -O /etc/apt/trusted.gpg.d/php.gpg
sh -c 'echo "deb $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'
apt-get update && apt-get install php7.4

Now run php -v to check your version.

If you need to install additional extensions: apt-get install php7.4-<extension-name>

In case you are running Apache2 and still have an old PHP version installed:

a2dismod php*.* # Replace *.* with your version number
a2enmod php7.4
systemctl restart apache2

Fixing this site for PHP 7.4

Tue 03, Dec 2019

Edit, a few minutes later: This site seems to be compatible (after a few minor fixes with the configuration), but my (a few months old) installation of Nextcloud is still incompatible. I'd have to update Nextcloud (and hope that all my addons are still supported), but I don't want to bother with that now.


PHP 7.4 was released a few days ago. I just checked if my websites and other webapps are compatible with it.

No, they are not.

Well, guess I have to start searching for the error (-s) now.

My Nextcloud and Roundcube installations are incompatible too btw. ._.

The Discord Reddit bot

Sat 30, Nov 2019

Today a friend and I created a simple Discord bot to list posts from different subreddits. The usage is really simple: !reddit <SUBREDDIT>

We already have a private, self-hosted instance of it, but we also provide its source code on GitHub.

PHP 7.4 released!

Thu 28, Nov 2019

PHP 7.4 has just been released! There are a lot of cool new features, such as class properties supporting type declarations or unpacking inside arrays.

You can read the full list of new features here!

You are on page 1 right now.

Back to top