How to get the "return type" of a function?

How to get the "return type" of a function?

am 23.02.2010 10:28:05 von Dasn

Hello guys, I try to use 'ReflectionFunction' to retrieve the info of a
function.
For example:

$rf = new ReflectionFunction('strstr');
echo $rf;
?>
=============== output ==================

Function [ function strstr ] {

- Parameters [3] {
Parameter #0 [ $haystack ]
Parameter #1 [ $needle ]
Parameter #2 [ $part ]
}
}

The problem is there's no 'return type' (i.e. 'string' in this example)
info about the function.

Could you tell me how to retrieve the 'return type'?
Thanks.


--
Dasn


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: How to get the "return type" of a function?

am 23.02.2010 13:40:49 von hSiplu

2010/2/23 Dasn :
> Hello guys, I try to use 'ReflectionFunction' to retrieve the info of a
> function.
> For example:
> >
> $rf =3D new ReflectionFunction('strstr');
> echo $rf;
> ?>
> ===============3D output =======
============
>
> Function [ function strstr ] {
>
>  - Parameters [3] {
>    Parameter #0 [ $haystack ]
>    Parameter #1 [ $needle ]
>    Parameter #2 [ $part ]
>  }
> }
>
> The problem is there's no 'return type' (i.e. 'string' in this example)
> info about the function.
>
> Could you tell me how to retrieve the 'return type'?
> Thanks.
I think PHP doesnt support it.

In ReflectionParameter class you'll see there is no parameter type too.

May be this is because PHP is loosely typed language.

--=20
Shiplu Mokaddim
My talks, http://talk.cmyweb.net
Follow me, http://twitter.com/shiplu
SUST Programmers, http://groups.google.com/group/p2psust
Innovation distinguishes bet ... ... (ask Steve Jobs the rest)

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: How to get the "return type" of a function?

am 23.02.2010 15:17:52 von Daniel Egeberg

2010/2/23 Dasn :
> Hello guys, I try to use 'ReflectionFunction' to retrieve the info of a
> function.
> For example:
> >
> $rf =3D new ReflectionFunction('strstr');
> echo $rf;
> ?>
> ===============3D output =======
============
>
> Function [ function strstr ] {
>
>  - Parameters [3] {
>    Parameter #0 [ $haystack ]
>    Parameter #1 [ $needle ]
>    Parameter #2 [ $part ]
>  }
> }
>
> The problem is there's no 'return type' (i.e. 'string' in this example)
> info about the function.
>
> Could you tell me how to retrieve the 'return type'?
> Thanks.
>
>
> --
> Dasn

That's not possible. Consider this function:

function foo()
{
switch (rand(0, 1)) {
case 0: return 42;
case 1: return 'bar';
}
}

What should the return type be?

--=20
Daniel Egeberg

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: How to get the "return type" of a function?

am 23.02.2010 15:22:46 von Bruno Fajardo

2010/2/23 Daniel Egeberg
>
> 2010/2/23 Dasn :
> > Hello guys, I try to use 'ReflectionFunction' to retrieve the info of a
> > function.
> > For example:
> > > >
> > $rf =3D new ReflectionFunction('strstr');
> > echo $rf;
> > ?>
> > ===============3D output =======
============
> >
> > Function [ function strstr ] {
> >
> > =A0- Parameters [3] {
> > =A0 =A0Parameter #0 [ $haystack ]
> > =A0 =A0Parameter #1 [ $needle ]
> > =A0 =A0Parameter #2 [ $part ]
> > =A0}
> > }
> >
> > The problem is there's no 'return type' (i.e. 'string' in this example)
> > info about the function.
> >
> > Could you tell me how to retrieve the 'return type'?
> > Thanks.
> >
> >
> > --
> > Dasn
>
> That's not possible. Consider this function:
>
> function foo()
> {
> =A0 =A0 =A0 =A0switch (rand(0, 1)) {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0case 0: return 42;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0case 1: return 'bar';
> =A0 =A0 =A0 =A0}
> }
>
> What should the return type be?

Mixed? http://www.php.net/manual/en/language.pseudo-types.php#langu age.type=
s.mixed

>
> --
> Daniel Egeberg
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: How to get the "return type" of a function?

am 23.02.2010 15:31:43 von TedD

At 3:17 PM +0100 2/23/10, Daniel Egeberg wrote:
>2010/2/23 Dasn :
> > Could you tell me how to retrieve the 'return type'?
>> Thanks.
>>
>>
>> --
>> Dasn
>
>That's not possible. Consider this function:
>
>function foo()
>{
> switch (rand(0, 1)) {
> case 0: return 42;
> case 1: return 'bar';
> }
>}
>
>What should the return type be?
>
>--
>Daniel Egeberg


It can be anything you want to test for -- check out:

is_int();
is_nan();
is_float();
is_long();
is_string();

IOW, is_whatever();

Cheers,

tedd

--
-------
http://sperling.com http://ancientstones.com http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: How to get the "return type" of a function?

am 23.02.2010 16:44:17 von Nathan Rixham

tedd wrote:
> At 3:17 PM +0100 2/23/10, Daniel Egeberg wrote:
>> 2010/2/23 Dasn :
>> > Could you tell me how to retrieve the 'return type'?
>>> Thanks.
>>>
>>>
>>> --
>>> Dasn
>>
>> That's not possible. Consider this function:
>>
>> function foo()
>> {
>> switch (rand(0, 1)) {
>> case 0: return 42;
>> case 1: return 'bar';
>> }
>> }
>>
>> What should the return type be?
>>
>> --
>> Daniel Egeberg
>
>
> It can be anything you want to test for -- check out:
>
> is_int();
> is_nan();
> is_float();
> is_long();
> is_string();
>
> IOW, is_whatever();
>
> Cheers,
>
> tedd
>

As PHP is loosely typed, the only real way around this is to specify a
return type in a PHPDoc block, then parse that using reflection to get
the @return parameter.

another option is to use something like haXe which is an ECMA style
typed language that compiles to multiple targets, one of which is PHP.

Regards!

Nathan


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: How to get the "return type" of a function?

am 24.02.2010 01:57:05 von Ashley Sheridan

--=-dzBpOXUnfBdEn7SQ46NS
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Tue, 2010-02-23 at 09:31 -0500, tedd wrote:

> At 3:17 PM +0100 2/23/10, Daniel Egeberg wrote:
> >2010/2/23 Dasn :
> > > Could you tell me how to retrieve the 'return type'?
> >> Thanks.
> >>
> >>
> >> --
> >> Dasn
> >
> >That's not possible. Consider this function:
> >
> >function foo()
> >{
> > switch (rand(0, 1)) {
> > case 0: return 42;
> > case 1: return 'bar';
> > }
> >}
> >
> >What should the return type be?
> >
> >--
> >Daniel Egeberg
>
>
> It can be anything you want to test for -- check out:
>
> is_int();
> is_nan();
> is_float();
> is_long();
> is_string();
>
> IOW, is_whatever();
>
> Cheers,
>
> tedd
>
> --
> -------
> http://sperling.com http://ancientstones.com http://earthstones.com
>


is_quantum() is pretty useful as well, if you want to see if it's sort
of there and not at the same time. Probably turns into a cat in a box at
some point too, everything quantum has cats in...

Thanks,
Ash
http://www.ashleysheridan.co.uk



--=-dzBpOXUnfBdEn7SQ46NS--

Re: How to get the "return type" of a function?

am 24.02.2010 02:16:19 von Ashley Sheridan

--=-M+MiUapws4eT9h+DTt/m
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Tue, 2010-02-23 at 19:19 -0600, Kevin Kinsey wrote:

> Ashley Sheridan wrote:
> > is_quantum() is pretty useful as well, if you want to see if it's sort
> > of there and not at the same time. Probably turns into a cat in a box at
> > some point too, everything quantum has cats in...
> >
> > Thanks,
> > Ash
>
> So, should we add to the list:
>
> is_schrodingers_cat_alive()
>
> ??
>
> KDK
>


I think PHP would crash trying to return the boolean value from that
one!

Thanks,
Ash
http://www.ashleysheridan.co.uk



--=-M+MiUapws4eT9h+DTt/m--

Re: How to get the "return type" of a function?

am 24.02.2010 02:19:01 von Kevin Kinsey

Ashley Sheridan wrote:
> is_quantum() is pretty useful as well, if you want to see if it's sort
> of there and not at the same time. Probably turns into a cat in a box at
> some point too, everything quantum has cats in...
>
> Thanks,
> Ash

So, should we add to the list:

is_schrodingers_cat_alive()

??

KDK

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: How to get the "return type" of a function?

am 24.02.2010 07:31:32 von Dasn

On Tue, 23 Feb 2010 23:44:17 +0800, Nathan Rixham wrote:


>
> As PHP is loosely typed, the only real way around this is to specify a
> return type in a PHPDoc block, then parse that using reflection to get
> the @return parameter.
>
> another option is to use something like haXe which is an ECMA style
> typed language that compiles to multiple targets, one of which is PHP.
>

Thanks Nathan for your comment. But I think the PHPDoc stuff is only for
user defined functions, right? The Reflection::getDocComment() has no idea
about the built-in functions.

--
Dasn


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: How to get the "return type" of a function?

am 24.02.2010 08:42:32 von Jochem Maas

Op 2/24/10 1:16 AM, Ashley Sheridan schreef:
> On Tue, 2010-02-23 at 19:19 -0600, Kevin Kinsey wrote:
>
>> Ashley Sheridan wrote:
>>> is_quantum() is pretty useful as well, if you want to see if it's sort
>>> of there and not at the same time. Probably turns into a cat in a box at
>>> some point too, everything quantum has cats in...
>>>
>>> Thanks,
>>> Ash
>>
>> So, should we add to the list:
>>
>> is_schrodingers_cat_alive()
>>
>> ??
>>
>> KDK
>>
>
>
> I think PHP would crash trying to return the boolean value from that
> one!

no. either it returns a random boolean, or the func signature is missing a
boolean $weOpenedTheBox parameter ... in which case it should be fully deterministic. :)

> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>
>


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php