DEV Community

Discussion on: 5 tips in PHP 🐘

Collapse
 
lito profile image
Lito
    private function getAccessor(): PropertyAccess
    {
        return $this->propertyAccessor ?? $this->propertyAccessor = PropertyAccess::createPropertyAccessor(); 
    }
Enter fullscreen mode Exit fullscreen mode

Is same as:

    private function getAccessor(): PropertyAccess
    {
        return $this->propertyAccessor ??= PropertyAccess::createPropertyAccessor(); 
    }
Enter fullscreen mode Exit fullscreen mode
Collapse
 
saymon profile image
Saymon Tavares

thanks guy!

Collapse
 
nfcristea profile image
Florin Cristea • Edited

??= will also assign the value on first call, besides checking if it's set, meaning that the second time it gets reached it will have a value assigned and PropertyAccess::createPropertyAccessor() will no longer get called.
?? is just a classic isset check meaning the call to createProperyAccesor() is made every time it gets reached.
They're quite different although they look similar.