Exciting Features to Look Forward in PHP 7.4

Have you ever wonder? what’s new in PHP. After PHP 7, PHP 7.4 is about to release on November 28th, 2019. No alpha or beta version, PHP 7.4 will be getting released as General Availability in the market.

The most exciting part will be to see the new features in this version that will make PHP website development much reliable and faster. 

Though it has already been approved that PHP 8 is the real milestone for PHP performance, still it’s quite far from being released. 

php 7.4

Here we will be focusing on some of the new features that you will be getting with PHP 7.4.

Constructors & Defaulters

Let’s take a keen look at how typed values are initialised. If the value is scalar types, it is possible to provide the default value: 

class Foo{public int $bar = 4;
public ?string $baz = null;
public array $list = [1, 2, 3];}

If the type is nullable then you can only use “null” as a default. Well, this is quite obvious to understand but there is some uneven behaviour with the parameter defaults where the following is included:

function passNull(int $i = null)
/* … */ } passNull(null);

Fortunately, this uneven behaviour with typed properties is not allowed. Also with “object” or class types, it is quite impossible to get default values. To set their defaults, you will have to use constructors. To initialise the typed values, usage of constructors is obvious:

class Foo
{
private int $a;public function __construct(int $a)
{
$this->a = $a;
}
}

However, you will have to make sure that writing uninitialised property outside the constructor is valid. The uninitialised check will not perform until there is something to read within the property.

Uninitialised

Is the following code valid or not?

class Foo
{
public int $bar;
}
$foo = new Foo;

PHP will display an error message when “$bar” is accessed even if its value is not an integer after making an object of “Foo”:

var_dump($foo->bar);
Fatal error: Uncaught Error: Typed property Foo::$barmust not be accessed before initialization

As you can see there is a new type of “State Variable” called uninitialised.

The value will be “null” if the “$bar” does not have any value. It is impossible to acknowledge whether the type nullable property was set or is forgotten if the “types” are nullable. This is because the new variable “uninitialised” will be introduced in PHP 7.4.

Let’s take a look at some of the major points to remember about uninitialised:
  • Reading from uninitialised properties will result in displaying a fatal error message.
  • If the type is non-nullable, you will be able to create an object within the uninitialised property as this state is verified while accessing the property.
  • Before reading, you can create an uninitialised property.
  • Unsetting an untyped property will make the value “null” but if using “unset” on a typed property then the value will be uninitialized.

You can keep a note on the below-mentioned code where the non-nullable and uninitialised property is set just after creating the object is valid:

class Foo
{
public int $a;
}
$foo = new Foo;
$foo->a = 1;

Type validation is done at the time of writing and uninitialised state is only analysed at the time of reading the value of the property. This makes it sure that no invalid type will end up as a property’s value.

Inheritance & Type Variance

It does not matter if PHP 7.4 has improved type variance, the typed properties are still the same i.e., invariant. For a better explanation, take a look at the following code:

class A {}
class B extends A {}
class Foo
{
public A $prop;
}
class Bar extends Foo
{
public B $prop;
}
Fatal error: Type of Bar::$prop must be A (as in class Foo)

If the above-mentioned example does not make any difference then consider taking a look at the following:

class Foo
{
public self $prop;
}
class Bar extends Foo
{
public self $prop;
}

This new version of PHP will be replacing “self” in the backend with the child class a while before executing the code. The only solution to handle it is mentioned below:

class Foo
{
public Foo $prop;
}
class Bar extends Foo
{
public Foo $prop;
}

If we talk about inheritance, it will be difficult for you to come up with any better use of cases to replace the types of inherited properties.

It is quite strange to see that it is possible to change the type of every individual inherited property. However, it is only possible if the access modifier is changed from “private” to “protected” or “public”.

Take a look at the following valid code:
class Foo
{
private int $prop;
}
class Bar extends Foo
{
public string $prop;
}

But, changing the type from nullable to non-nullable or vice-versa is not allowed in this version of PHP i.e., 7.4.

class Foo
{
public int $a;
public ?int $b;
}
class Bar extends Foo
{
public ?int $a;
public int $b;
}

Fatal error: Type of Bar::$a must be int (as in class Foo)

Types

Let’s see what can be typed and in which form. However, make sure that typed properties work underclasses. For making them work, access modifier or keyword “var” is required in front of them.

In PHP 7.4, almost every type can be used excluding “void” & “callable”. “void” refers to the absence of a value which is quite clear that it cannot be used to type any value. Similarly with “callable”, it seems nuanced.

The following code shows how a “callable” looks like in PHP:

class Foo
{
public callable $callable;public function __construct(callable $callable)
{ /* … */ }
}
class Bar
{
public Foo $foo;
public function __construct()
{
$this->foo = new Foo([$this, ‘method’])
}
private function method()
{ /* … */ }
}
$bar = new Bar; ($bar->foo->callable)();

Here, “callable” is a private “Bar: :method”, though it is contexting the “Foo”. Due to this problem, PHP 7.4 do not include “callable” support.

However, it’s not a big deal as “Closure” is included as a valid type in PHP 7.4 that will call “$this” context when required.

Here’s a list of types that will be available in PHP 7.4:

● int
● float
● bool
● aray
● string
● iterable
● object
● self & parent
● ? (nullable)
● Classes & interfaces

Strict Types & Coercion

PHP is known to be a dynamic programming language to which many developers loves and even hate. If you are passing a string where you expect it an integer, current PHP will approach and will convert it into a string automatically:

function coerce(int $i)
{ /* … */ }coerce(‘1’); // 1

The same applies on the typed properties. Take a look at the following valid code which is converting “1” to 1.

class Bar
{
public int $i;
}
$bar = new Bar;
$bar->i = ‘1’; // 1

The best thing is if you do not want this behaviour, you can simply disable it by applying strict types:

declare(strict_types=1);
$bar = new Bar;
$bar->i = ‘1’; // 1

Fatal error: Uncaught TypeError:
Typed property Bar::$i must be int, string used

Well, these are some of the important types of properties that you will see in the new update of PHP i.e., PHP 7.4 GA release. Feel free to contact us to know more about PHP web development service.

Written By

admin