DEV Community

Discussion on: PHP 7.4 in prod at last

Collapse
 
felixdorn profile image
Félix Dorn • Edited

I always prefer LTS versions, less bugs, less unexpected crap happening. But I would love to use PHP7.4 tought, especially Typed Variables.

Collapse
 
petemcfarlane profile image
Pete McFarlane

I think all versions of PHP are supported for 2 years now: php.net/supported-versions.php
I'm not sure any of them are LTS any more?
Typed properties are nice, I like that you have to be explicit if a property could also be null too, for example

class Foo
{
    /** @var Bar */
    private $bar;

    public function __construct(Bar $bar = null) 
    {
        $this->bar = $bar;
    }
}

was perfectly legal but you never knew if $bar was a Bar or null.

Now if I have

class Foo
{
    private Bar $bar;

    public function __construct(Bar $bar = null) 
    {
        $this->bar = $bar;
    }
}

it will throw a TypeError if I try to construct a new instance of Foo with a null value for $bar. To handle this I change the type to be ?Bar like so:

class Foo
{
    private ?Bar $bar;

    public function __construct(Bar $bar = null)
    {
        $this->bar = $bar;
    }
}

This then forces me to think when using $this->bar that the value could be null and handle appropriately!