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

From: Date: Wed, 05 Mar 2014 22:51:53 +0000
Subject: RE: [PHP] Mixed return type - boolean and array/resource/closure or object
References: 1 2 3 4 5 6 7 8  Groups: php.general 
Request: Send a blank email to php-general+get-323038@lists.php.net to get a copy of this message
> -----Original Message----- > From: Lester Caine [mailto:lester@lsces.co.uk] > Sent: Wednesday, March 05, 2014 11:15 PM > To: php-general@lists.php.net > 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. > > > 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. > I try as much as possible to list the various return options in the docblock > function header which is fine where I'm working with everything via an IDE which > simply uses it as a type hint. This goes back to PHP4 days and still works > perfectly well without needing anything more complex. Tweaking the formal help > to match that works for 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. > Data returned from databases uses 'null' for the specific instance of 'not > set' > and while you could wrap every data field into an object, simply replacing a > value with 'null' in a results array is totally logical. Binary has two values - > 'yes', 'no' and 'I don't know' :) > > -- > Lester Caine - G8HFL I think you wander off the subject. We were talking about built-in functions/methods and not about userland functions/methods. The original question was if a function/method exists which returns either a boolean or array/resource/closure/object and I got already helpful answers - thanks again :) And the last bit of the discussion was just if the documentation should have "mixed" as return type for such functions/methods or "array/resource/closure/a specific object"

« previous php.general (#323038) next »