Discussion:
Mixed return type - boolean and array/resource/closure or object
Robert Stoll
2014-03-03 23:10:48 UTC
Permalink
Heya,

I would like to know if a PHP built-in function/method exists which returns one of the following combinations
- a boolean or an array
- a boolean or a resource
- a boolean or a closure
- a boolean or an object

I searched in the language manual, but it is quite hard to be honest since there is not really an overview.
Please get back to me if you are certain that there is not such a function/method or give me an example if there is one.

Cheers,
Robert
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Aziz Saleh
2014-03-04 00:05:07 UTC
Permalink
Hey Aziz
Thanks for your response, but you got my question wrong.
I wonder if there is a built-in function which returns one of the depicted
combinations.
Like strpos which returns either int or false (Boolean)
Cheers,
Robert
*Sent:* Tuesday, March 04, 2014 12:20 AM
*To:* Robert Stoll
*Subject:* Re: [PHP] Mixed return type - boolean and
array/resource/closure or object
Heya,
I would like to know if a PHP built-in function/method exists which
returns one of the following combinations
- a boolean or an array
- a boolean or a resource
- a boolean or a closure
- a boolean or an object
I searched in the language manual, but it is quite hard to be honest since
there is not really an overview.
Please get back to me if you are certain that there is not such a
function/method or give me an example if there is one.
Cheers,
Robert
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
All of what you need can be found here (is_bool, is_resource, is_object,
is_array)
http://www.php.net/manual/en/ref.var.php
The closure is just an object of type Closure, so is_object($object) &&
get_class($object) == 'Closure' should be sufficient to check for closures.
Aziz
Hi Robert,

Sorry I hit reply instead of reply all.

Afaik, there isn't a function that would do what you need. You will need to
make your own implementation containing the combination you need.

Aziz
Christoph Becker
2014-03-04 00:26:36 UTC
Permalink
Post by Robert Stoll
I would like to know if a PHP built-in function/method exists which returns one of the following combinations
- a boolean or an array
- a boolean or a resource
- a boolean or a closure
- a boolean or an object
I searched in the language manual, but it is quite hard to be honest since there is not really an overview.
Please get back to me if you are certain that there is not such a function/method or give me an example if there is one.
Off the top of my head, I can only name functions that return
resource/boolean, such as fopen(). But as boolean might be used as
failure indicator, there may be functions with the other return types.

BTW: what do you need that information for?
--
Christoph M. Becker
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Robert Stoll
2014-03-04 09:42:39 UTC
Permalink
-----Original Message-----
Sent: Tuesday, March 04, 2014 1:27 AM
Subject: Re: Mixed return type - boolean and array/resource/closure or object
Post by Robert Stoll
I would like to know if a PHP built-in function/method exists which returns one of the following combinations
- a boolean or an array
- a boolean or a resource
- a boolean or a closure
- a boolean or an object
I searched in the language manual, but it is quite hard to be honest since there is not really an overview.
Please get back to me if you are certain that there is not such a function/method or give me an example if there is
one.
Off the top of my head, I can only name functions that return
resource/boolean, such as fopen(). But as boolean might be used as
failure indicator, there may be functions with the other return types.
BTW: what do you need that information for?
--
Christoph M. Becker
Thank you, I see that the signature in the documentation is not appropriate. My first thought was also fopen but well,
the documentation confused me :)
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jim Lucas
2014-03-04 00:45:28 UTC
Permalink
Post by Robert Stoll
Heya,
I would like to know if a PHP built-in function/method exists which returns one of the following combinations
- a boolean or an array
- a boolean or a resource
- a boolean or a closure
- a boolean or an object
I searched in the language manual, but it is quite hard to be honest since there is not really an overview.
Please get back to me if you are certain that there is not such a function/method or give me an example if there is one.
Cheers,
Robert
What is it you are trying to do?
--
Jim Lucas

http://www.cmsws.com/
http://www.cmsws.com/examples/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Robert Stoll
2014-03-04 09:45:28 UTC
Permalink
-----Original Message-----
Sent: Tuesday, March 04, 2014 1:45 AM
Subject: Re: [PHP] Mixed return type - boolean and array/resource/closure or object
Post by Robert Stoll
Heya,
I would like to know if a PHP built-in function/method exists which returns one of the following combinations
- a boolean or an array
- a boolean or a resource
- a boolean or a closure
- a boolean or an object
I searched in the language manual, but it is quite hard to be honest since there is not really an overview.
Please get back to me if you are certain that there is not such a function/method or give me an example if there is
one.
Post by Robert Stoll
Cheers,
Robert
What is it you are trying to do?
--
Jim Lucas
http://www.cmsws.com/
http://www.cmsws.com/examples/
I need the information for my project Type-Safe PHP => http://tsphp.ch
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
m***@behnke.biz
2014-03-04 07:21:55 UTC
Permalink
Post by Robert Stoll
I would like to know if a PHP built-in function/method exists which returns
one of the following combinations
- a boolean or an array
- a boolean or a resource
- a boolean or a closure
- a boolean or an object
I searched in the language manual, but it is quite hard to be honest since
there is not really an overview.
Please get back to me if you are certain that there is not such a
function/method or give me an example if there is one.
Although PHP is capable of returning different types of values, you should not
do it.
Post by Robert Stoll
Cheers,
Robert
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
Marco Behnke
Dipl. Informatiker (FH), SAE Audio Engineer Diploma
Zend Certified Engineer PHP 5.3

Tel.: 0174 / 9722336
e-Mail: ***@behnke.biz

Softwaretechnik Behnke
Heinrich-Heine-Str. 7D
21218 Seevetal

http://www.behnke.biz
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sebastian Krebs
2014-03-05 02:17:33 UTC
Permalink
Post by Robert Stoll
Heya,
I would like to know if a PHP built-in function/method exists which
returns one of the following combinations
- a boolean or an array
- a boolean or a resource
- a boolean or a closure
- a boolean or an object
I searched in the language manual, but it is quite hard to be honest since
there is not really an overview.
Please get back to me if you are certain that there is not such a
function/method or give me an example if there is one.
Some array functions like "current", "array_pop" and so on (item of
arbitrary type or null) can return everything. An example for "boolean or
object" is 'date_create' [1]. Afaik there is no built-in function, that
returns a closure, so the combination "boolean or closure" is not possible
at all.


[1] http://php.net/datetime.construct
Post by Robert Stoll
Cheers,
Robert
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
github.com/KingCrunch
Robert Stoll
2014-03-05 08:52:22 UTC
Permalink
From: Sebastian Krebs [mailto:***@gmail.com]
Sent: Wednesday, March 05, 2014 3:18 AM
To: Robert Stoll
Cc: PHP General List
Subject: Re: [PHP] Mixed return type - boolean and array/resource/closure or object
Some array functions like "current", "array_pop" and so on (item of arbitrary type or null) can return everything. An
example for "boolean or object" is 'date_create' [1]. Afaik there is no built-in function, that returns a closure, so
the combination "boolean or closure" is not possible at all.

Hm... date_create is an alias of DateTime::__construct and as far as I understood __construct returns usually an object
and false only in the case where an exception has been thrown anyway. Right?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sebastian Krebs
2014-03-05 09:24:23 UTC
Permalink
Post by Robert Stoll
Sent: Wednesday, March 05, 2014 3:18 AM
To: Robert Stoll
Cc: PHP General List
Subject: Re: [PHP] Mixed return type - boolean and array/resource/closure or object
Post by Sebastian Krebs
Some array functions like "current", "array_pop" and so on (item of
arbitrary type or null) can return everything. An
example for "boolean or object" is 'date_create' [1]. Afaik there is no
built-in function, that returns a closure, so
the combination "boolean or closure" is not possible at all.
Hm... date_create is an alias of DateTime::__construct and as far as I
understood __construct returns usually an object
and false only in the case where an exception has been thrown anyway. Right?
It isn't directly an alias, because one is a method and the other one a
function ;) OK, thats too easy, but that makes the difference: In case of
an error DateTime::__construct() emits an Exception (OOP-like), whereas
date_create() proceeds, but return 'false' (functional-like).
--
github.com/KingCrunch
Robert Stoll
2014-03-05 09:37:20 UTC
Permalink
-----Original Message-----
Sent: Wednesday, March 05, 2014 10:24 AM
To: Robert Stoll
Cc: PHP General List
Subject: Re: [PHP] Mixed return type - boolean and array/resource/closure or object
Post by Robert Stoll
Sent: Wednesday, March 05, 2014 3:18 AM
To: Robert Stoll
Cc: PHP General List
Subject: Re: [PHP] Mixed return type - boolean and array/resource/closure or object
Post by Sebastian Krebs
Some array functions like "current", "array_pop" and so on (item of
arbitrary type or null) can return everything. An
example for "boolean or object" is 'date_create' [1]. Afaik there is no
built-in function, that returns a closure, so
the combination "boolean or closure" is not possible at all.
Hm... date_create is an alias of DateTime::__construct and as far as I
understood __construct returns usually an object
and false only in the case where an exception has been thrown anyway. Right?
It isn't directly an alias, because one is a method and the other one a
function ;) OK, thats too easy, but that makes the difference: In case of
an error DateTime::__construct() emits an Exception (OOP-like), whereas
date_create() proceeds, but return 'false' (functional-like).
--
github.com/KingCrunch
I see, thanks. A little bit confusing that this information is located at DateTime::__construct() and not directly at
date_create().
Nevertheless, the signature in the documentation implies that date_create() returns always DateTime.
I already submitted a patch to change it to mixed :)
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Christoph Becker
2014-03-05 16:36:49 UTC
Permalink
Post by Robert Stoll
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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Robert Stoll
2014-03-05 21:44:48 UTC
Permalink
-----Original Message-----
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
Post by Robert Stoll
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.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Lester Caine
2014-03-05 22:14:37 UTC
Permalink
Post by Robert Stoll
Post by Christoph Becker
Post by Robert Stoll
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.
Post by Robert Stoll
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
-----------------------------
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk
Rainbow Digital Media - http://rainbowdigitalmedia.co.uk
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Robert Stoll
2014-03-05 22:51:53 UTC
Permalink
-----Original Message-----
Sent: Wednesday, March 05, 2014 11:15 PM
Subject: Re: [PHP] Mixed return type - boolean and array/resource/closure or object
Post by Robert Stoll
Post by Christoph Becker
Post by Robert Stoll
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
Post by Robert Stoll
strpos as a reference (it has mixed as return type). Yet, mixed is not really precise and I think it would be better
to
Post by Robert Stoll
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
Post by Robert Stoll
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.
Post by Robert Stoll
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"
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Lester Caine
2014-03-06 20:59:24 UTC
Permalink
Post by Robert Stoll
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"
My point(s) were that the documentation for built in functions is not has
helpful as CAN be achieved by wrapping those functions in 'userland' methods :)
'mixed' is never the right hint for any of the internal functions, as 'values',
'boolean' and 'null' all provide particular states of result. While some people
might prefer that errors are returned as exceptions, returning 'failure' in
procedural programming is also how PHP works, and the documentation simply needs
to identify those returns.
--
Lester Caine - G8HFL
-----------------------------
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk
Rainbow Digital Media - http://rainbowdigitalmedia.co.uk
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Christoph Becker
2014-03-06 12:30:56 UTC
Permalink
Post by Robert Stoll
-----Original Message-----
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
Post by Robert Stoll
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.
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.
I was referring to the following note regarding built-in functions[1]:

| If the parameters given to a function are not what it expects, such
| as passing an array where a string is expected, the return value of
| the function is undefined. In this case it will likely return NULL
| but this is just a convention, and cannot be relied upon.

[1] <http://www.php.net/manual/en/functions.internal.php>
--
Christoph M. Becker
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Robert Stoll
2014-03-06 18:21:43 UTC
Permalink
-----Original Message-----
Sent: Thursday, March 06, 2014 1:31 PM
To: Robert Stoll; 'Sebastian Krebs'
Cc: 'PHP General List'
Subject: Re: [PHP] Mixed return type - boolean and array/resource/closure or object
Post by Robert Stoll
-----Original Message-----
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
Post by Robert Stoll
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
Post by Robert Stoll
strpos as a reference (it has mixed as return type). Yet, mixed is not really precise and I think it would be better
to
Post by Robert Stoll
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
Post by Robert Stoll
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.
| If the parameters given to a function are not what it expects, such
| as passing an array where a string is expected, the return value of
| the function is undefined. In this case it will likely return NULL
| but this is just a convention, and cannot be relied upon.
[1] <http://www.php.net/manual/en/functions.internal.php>
--
Christoph M. Becker
Cheers, I wasn't aware of this fact.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Loading...