Re: Implicit isset/isempty check on short-ternary operator

From: Date: Thu, 14 Apr 2011 12:08:44 +0000
Subject: Re: Implicit isset/isempty check on short-ternary operator
References: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20  Groups: php.internals 
Request: Send a blank email to internals+get-51893@lists.php.net to get a copy of this message
On 14.04.2011 13:58, Ben Schmidt wrote: > I have many, many uses for this. E.g. patterns like this: > > class Foo { > private $defaultBar; > public function foobar(Bar $bar=null) { > $bar = isset($bar) ? $bar : $this->defaultBar; > $bar->doSomething(); > } > } I'm sorry but this is not a valid use case, isset() is totally unnecessary here. You can use $bar = $bar ?: $this->defaultBar; Of course it still repeats $bar, but it's not that verbose, and will work exactly the same as what you do. I'd argue it's even better than what you do because, if it weren't for that Bar type hint, if someone passes false or "blah" to your function, isset() will return true, and you'll try to call doSomething on false/"blah", which is a cute fatal error. Cheers -- Jordi Boggiano @seldaek :: http://seld.be/

« previous php.internals (#51893) next »