Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Ubuntu 18.04 - Install PHP 7.4

PHP

Steps

sudo add-apt-repository ppa:ondrej/php -y
sudo apt update && sudo apt install php7.4 -y

Key New Features

Typed Properties!

E.g.

class User {
    private int $id;
    private string $name;
    private FooClass $foo;
}

Allow exceptions from __toString()

Throwing exceptions from __toString() is now permitted. Previously this resulted in a fatal error. Existing recoverable fatal errors in string conversions have been converted to Error exceptions. This is important to me because of my use of the AbstractView package.

Unpacking Inside Arrays

$parts = ['apple', 'pear'];
$fruits = ['banana', 'orange', ...$parts, 'watermelon']; // ['banana', 'orange', 'apple', 'pear', 'watermelon'];

Limited return type covariance and argument type contravariance

Even though the return types are different for the same method declaration, this will work because B extends A, so is a super type.

class A {}
class B extends A {}

class Producer {
    public function method(): A {}
}
class ChildProducer extends Producer {
    public function method(): B {}
}

References

Last updated: 17th March 2020
First published: 17th March 2020