Return null from a link return function

If you have a function that returns a reference to an object, and it should return null if the object for some reason does not exist. What is the best way to do this?

If you just return null, as usual, you will receive Only variable references should be returned by reference.

One way to do something like

$null = null;
return $null;

But this does not seem very pleasant. Of course, one way would be to throw an exception instead of returning null. But if we want to return nullexceptions instead of throwing exceptions, is there a better way?

+4
source share
1 answer

In my opinion, this works for me well.

class Opinion 
{
    protected $presetValue = null;

    public function &getValue()
    {
        return $this->presetValue;
    }
}

, , ,

0

Source: https://habr.com/ru/post/1607098/


All Articles