PHP8 - New features - Supported RFC

PHP8 - New features - Supported RFC

PHP 8, the new major PHP version, is expected to be released by the end of 2020. It's in very active development right now, so things are likely to change a lot in the upcoming months.

Some websites are already trying to create chaos around the subject, stating that PHP 8 might not come out. Mostly due to this post from Dmitry Stogov, Matthew Weier O’Phinney and Enrico Zimuel. But it doesn’t change much: PHP 8 development is still strong and fast-paced.

The Just In Time compiler is definitely the biggest deal for this version, bringing virtually no changes to how code looks like but potentially many unexpected behaviours to PHP platforms. So I strongly believe many previews will be delivered in 2020 way before we hit any alpha, to make sure all current features are at least stable.

New features

Starting with new features, remember that PHP 8 is still in active development, so this list will grow over time.

Union types

Given the dynamically typed nature of PHP, there are lots of cases where union types can be useful. Union types are a collection of two or more types which indicate that either one of those can be used.

public function foo(Foo|Bar $input): int|float;

Note that void can never be part of a union type, since it indicates "no return value at all". Furthermore, nullable unions can be written using |null, or by using the existing ? notation:

public function foo(Foo|null $foo): void;

public function bar(?Bar $bar): void;

JIT

The JIT — just in time — compiler promises significant performance improvements, albeit not always within the context of web requests. There haven't been any accurate benchmarks done at this point, but they sure will come.

If you want to know more about what the JIT can do for PHP, you can read another post I wrote about it here.

Static return type

While it was already possible to return self, static wasn't a valid return type until PHP 8. Given PHP's dynamically typed nature, it's a feature that will be useful to many developers.

class Foo
{
    public function test(): static
    {
        return new static();
    }
}

Weak maps

Built upon the weakrefs RFC that was added in PHP 7.4, a WeakMap implementation is added in PHP 8. WeakMaps hold references to objects, which don't prevent those objects from being garbage collected.

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 hundreds, if not thousands of entities within a request; weak maps can offer a better, more resource friendly way of dealing with these objects.

Here's what weak maps look like, an example from the RFC:

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

The ::class field on objects

A small, yet useful, new feature: it's now possible to use ::class on objects, instead of having to use get_class() on them. It works the same way as get_class().

$foo = new Foo();

var_dump($foo::class);

Stringable interface

The Stringable interface can be used to type hint anything that is a string or implements __toString(). Furthermore, whenever a class implements __toString(), it automatically implements the interface behind the scenes and there's no need to manually implement it.

class Foo
{
    public function __toString(): string
    {
        return 'foo';
    }
}

function bar(Stringable $stringable) { /* … */ }

bar(new Foo());
bar('abc');
مصطفی برمشوری

Just me, myself and I, exploring the universe of uknownment. I have a heart of love and a interest of lorem ipsum and mauris neque quam blog. I want to share my world with you.


{{title}}

{{description}}

Software development outsourcing: how to avoid contract loopholes

Signing a contract is the last stage in the vendor selection process. Once you have gone through the vendor evaluation and analysis activities, here comes the final step - forging a contract. To strike a win-win deal, businesses need to scrutinize the contract far and wide remaining no issue unsettled.

What is Outsourcing? 4 Common Things Companies Outsource

There has never been a better time to outsource work to other agencies in order to save money and/or time on projects that will give your business a boost. So what are some outsourcing examples for you to fully understand the scope of the trend?

Wordpress Professional Editorial Board

WordPress Professional Editorial Board is a dashboard to monitor and manage publications on your WordPress sites. You can manage posts for editing, reviewing and publishing on one or more WordPress sites through one integrated dashboard.

How to Design Website Footer to Engage More Visitor

Aside from the main body content, a website includes a header and footer, which serve a particular purpose to help visitors. We believe that website footer design is just as important. First of all let’s answer two vital questions: What is a website footer? What should be in the website footer?

How does a switch learn a MAC address which is not in its lookup table?

Suppose the Switch just started, and it received a frame that contains a destination MAC address for a network device not in its MAC addresses table. What happens then? We describe it briefly.

How to download videos from Linkedin?

Here I show you how to simply download videos from LinkedIn to share it on any other social media. It could be done only by the some simple steps. It does not need to install any tools other than your browser (Chrome or Firefox)

Questions? Chat with us!Support is online.Support is away.Chat with ViraWeb123 TeamChat with ViraWeb123 Team