advertisement

[4] PHP 8.0 throw expression, inheritance with private methods, weak maps

Explore PHP 8.0 updates: throw expressions for concise error handling, improved inheritance mechanisms, and weak maps support.

This is the fourth part of the series and in the current post, I want to go through the Throw expression, Inheritance with private methods, and Weak maps. I am doing so because I want to keep up with the new features of PHP8. I know there are several other articles about this theme but I wanted to write down my notes about this topic because this way it stays in my memory longer. Anyway, you can see the full list of features here. Furthermore, you can read the third part of the series here.

Throw expression

Okay! Let's start with the definition. According to the Wikipedia, the difference between statement and expression is:

"In most languages, statements contrast with expressions in that statements do not return results and are executed solely for their side effects, while expressions always return a result and often do not have side effects at all."

// A statement
print('Hello, World.')
// An expression:
X=your data
print (X)

Currently or before PHP 8.0 throw was a statement. Changing the Throw statement to expression helps us to write structures like this:

$triggerError = fn () => throw new MyError();

$foo = $bar['offset'] ?? throw new OffsetDoesNotExist('offset');

Inheritance with private methods

Before PHP 8.0 the engine checked the public, protected and private methods signatures in the same way. However, this check was unnecessary because private methods won't be accessible by child classes. So there was extra not very useful check on private methods.

Weak maps

PHP 8.0 implements Weak maps upon Weak References functionality which was introduced in PHP 7.4.

"Weak References allow the programmer to retain a reference to an object which does not prevent the object from being destroyed; They are useful for implementing cache like structures. They are currently supported in PHP by extension."

Let's see when will be Weak maps useful:

"Take the example of ORMs, they often implement caches which hold references to entity classes to improve the performance of relations between entities. These entity objects can not be garbage collected, as long as this cache has a reference to them, even if the cache is the only thing referencing them."

"If this caching layer uses weak references and maps instead, PHP will garbage collect these objects when nothing else references them anymore. Especially in the case of ORMs, which can manage several hundred, if not thousands of entities within a request; weak maps can offer a better, more resource-friendly way of dealing with these objects."

class Foo 
{
    private WeakMap $cache;
 
    public function getSomethingWithCaching(object $obj): object
    {
        return $this->cache[$obj]
           ??= $this->computeSomethingExpensive($obj);
    }
}

New in PHP 8

The previous part of the series

[3] New PHP8 Constructor property promotion, static return, mixed type features

advertisement