What’s new in the upcoming PHP 7.4?

Good overview by Brent on the new features that will be landing in PHP 7.4, which is due somewhere December 2019.

I’m especially looking forward to Typed properties.

New in PHP 7.4 →

💭 One thing I personally find missing in PHP’s typehinting, is a way to typehint array contents — e.g. “I’d like an array of Foo‘s”. As posted before (2016) you could leverage the variadic function syntax, but that only works if all arguments of the function are of the same type or if the variadic parameter is the last argument.

The addition of a [] suffix on the hinted type – like Java has – could potentially solve this all:

// ⚠️ Fictional syntax! Does not work
function workWithFoo(Foo[] $foos, string $otherArgument) : void {
    foreach ($foos as $foo) {
        // etc.
    }
}

In the mean time I resort to hackery like this (src):

// Definition
class ArrayOfFoo extends \ArrayObject {
    public function offsetSet($key, $val) {
        if ($val instanceof Foo) {
            return parent::offsetSet($key, $val);
        }
        throw new \InvalidArgumentException('Value must be a Foo');
    }
}

// Usage
function workWithFoos(ArrayOfFoo $foos, string $otherArgument) : void {
    foreach ($foos as $foo) {
        // etc.
    }
}

$foos = new ArrayOfFoo();
$foos[] = new Foo();
$foos[] = new Foo();
workWithFoos($foos, 'foo');

If anyone has a better way to tackle this, feel free to enlighten me by leaving a comment.

PHP Scalar Type Hints, Follow-up

php-logo

The vote for the RFC “PHP Scalar Type Hints” got cancelled, after the author of the RFC – Andrea Faulds – posted “I Quit” on the internals list:

For my own reasons, I have decided to cease my involvement with PHP development. […] I’m no longer contributing to PHP, and all four active RFCs authored by myself are therefore withdrawn: Scalar Type Hints, Spaceship/Combined Comparison Operator, Void Return Type, Big Integer Support.

Very sad, yet it’s a choice one must respect.

Whilst the vote still was open, my eye fell on “Do NOT work around weak typehints, but fix them” by Bob Weinand, who voted “no”. His conclusion read:

[…] I voted no on the RFC. Not because it is a bad RFC nor a bad concept, it just tries to fix the problem the wrong way. It is a workaround around the too relax current type casting, which basically is the root problem to should be solved.

I think, working around the core problem is definitely not the way to go. Fix the core problem, then we don’t need strict scalar hints (as weak hints already will cover everything strict hints aim to solve) and unicorns stop dying so fast.

As someone who’s not familiar with how the voting system in the PHP community actually works this got me wondering why Bob’s argumentation made wasn’t noted somewhere along the RFC before the vote started. It’s a very reasonable argumentation, with some good points.

Now, whilst I do dig the gist of the RFC I wasn’t a fan of the suggested declare(strict_types=1) { … } myself either. If I were to vote on the RFC, it would have resulted in a “no” too, even though I like the essence of it. What seems to be missing on the vote is a “no, but …” (or even “yes, but …”) option, for people like Bob and me.

Getting lots of “no” votes is not pleasant, yet – when looking at the results of the ternary vote – I do suspect quite a few of those were more “no, but …” votes …

Perhaps there were too little possible options to vote for? Or perhaps the vote was opened too soon?

PHP Scalar Type Hints

php-logo

At this moment a vote on the PHP RFC “Scalar Type Hints” is going on. It would allow stuff like this:

<?php
class ElePHPant {
    public $name, $age, $cuteness, $evil;
    public function __construct(string $name, int $age, float $cuteness, bool $evil) {
        $this->name = $name;
        $this->age = $age;
        $this->cuteness = $cuteness;
        $this->evil = $evil;
    }
}

With the “Return Type Declarations” RFC passed, I’d find it logical to also let this vote pass. I hope they reach the 2/3 majority – which is required as the RFC proposes a change in the PHP language – by the time the vote closes (2015-02-19). With the current 2123 to 20 it looks like this feature won’t make it though.

UPDATE: This vote will not pass. As @SaraMG clarified:

A pity imho.

PHP RFC: Scalar Type Hints →
PHP RFC: Return Type Declarations →