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:

@@SomeAttribute
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!

Source

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

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:

<?php
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:

<?php
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 <meta http-equiv="refresh">.

Cookies

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

<?php
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!';
    }

}

Docs

Releasing my PHP framework

Fri 06, Mar 2020

Hi!

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 https://packages.sury.org/php/apt.gpg
sh -c 'echo "deb https://packages.sury.org/php/ $(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.


Hi

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