Re: Callable type

From: Date: Wed, 08 Jun 2011 10:17:38 +0000
Subject: Re: Callable type
References: 1 2 3 4 5 6 7 8  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
2011/6/8 Johannes Schlüter <[email protected]>:
> On Wed, 2011-06-08 at 10:38 +0200, Hannes Magnusson wrote:
>> 2011/6/8 Johannes Schlüter <[email protected]>:
>> > On Tue, 2011-06-07 at 12:12 -0700, Stas Malyshev wrote:
>> >> Hi!
>> >>
>> >> > https://wiki.php.net/rfc/callable
>> >>
>> >> It is good there's an RFC. However it seems to lack code examples. I
>> >> understand it may be obvious to the proposers how it looks like, but
>> >> it'd be nice to have the actual example there as it is done nearly
>> >> everywhere else.
>> >
>> > The RFC is missing information about what happens in codebases which
>> > already have a "callable" type declared. Will that be prevented or will
>> > they hit a runtime error? ("callable expected, callable type found")
>>
>> You mean an interface/class with that name?
>> The error would be 'expected instanceof callable, string/array/closure recieved.
>>
>> gettype("strpos") will still return a string, not callable.
>>
>> A callable wouldn't be fully featured type.
>
> Which means that
>   class callable { }

No. 'callable' is a parser token.



>> > What about default values? Will
>> >    function foo(callback $cb = 'strpos') { }
>> > be valid?
>>
>> No default values, other then NULL allowed.
>> Otherwise we would need to support array("classname", "methodname")
>> too, and then people would want default array values for array
>> typehinting etc etc etc.
>
> Ok. I assume NULL as default value would be allowed, though. This would
> be consistent for the language and allow such things:
>
> function foo(callback $cb = NULL) {
>    if (!$cb) {
>        $cb = function() { /* .. default implementation */
>    }
>    ....
> }

Etienne pointed out that default values for arrays can actually contain values..
I actually had no idea.

I suppose we then need to support default values for an array callable.

But yes, a callable can have the default value of null, following the
rules of class hinting.


-Hannes


Thread (63 messages)

« previous php.internals (#53199) next »