RE: [PHP] Mixed return type - boolean and array/resource/closure or object

From: Date: Wed, 05 Mar 2014 21:44:48 +0000
Subject: RE: [PHP] Mixed return type - boolean and array/resource/closure or object
References: 1 2 3 4 5 6  Groups: php.general 
Request: Send a blank email to php-general+get-323036@lists.php.net to get a copy of this message
> -----Original Message----- > From: Christoph Becker [mailto:cmbecker69@gmx.de] > Sent: Wednesday, March 05, 2014 5:37 PM > To: Robert Stoll; 'Sebastian Krebs' > Cc: 'PHP General List' > Subject: Re: [PHP] Mixed return type - boolean and array/resource/closure or object > > Robert Stoll wrote: > > > Nevertheless, the signature in the documentation implies that > > date_create() returns always DateTime. I already submitted a patch to > > change it to mixed :) > > IMHO, the signature is fine, as false is returned only in *exceptional* > cases. To be strict, changing the return type to mixed had to be done > for (nearly) *all* functions, because several may return false in error > cases, and many will return null when wrong arguments are passed. > > -- > Christoph M. Becker I see your point. But IMO it is confusing that something else is returned than mentioned in the signature. I always take strpos as a reference (it has mixed as return type). Yet, mixed is not really precise and I think it would be better to have something like "int|false" or a shorter form, maybe "int!" to indicate that either int or false is returned. Changing strpos to int would be wrong IMO. I want to see at one glance from the signature of the function what it returns. As I said mixed is not ideal either since I have to scroll down to 'Return Values' but better than confusing me. About null, I see null as part of an object type. I know that many people would not agree on this one but for me it makes sense.

« previous php.general (#323036) next »