> You could try some kind of stricter casting. Where odd conversions
between array/object <-> bool/int/float/string do not work on parameter
casting .. but then you end up with two types of casting ...

I have used in depth the casting operators of C#, which are incredibly
flexible and powerful, and they are based on this exact idea. In C#, there
are two types of casting: implicit and explicit. The first is used for
trivial conversions and the later for the "odd" ones. It is possible to
classify PHP's casts into these two categories:

int to string: implicit
int to float:  implicit
int to object: explicit
int to array:  explicit
...

Once this classification is done, the code becomes much more sane. Examples:

function test_float( float test ) { ... }
test_float( 1.0 );  // ok
test_float( 1 );    // implicit int to float cast, ok

function test_array( array test ) { ... }
test_array( array() );  // ok
test_array( 1 );        // no implicit int to array cast, error!
test_array( (array)1 ); // explicit int to array cast, ok

I deliberately avoided using Anthony's cast-like syntax, because it does
not fit nicely here. Calling a function may cause an implicit cast, but it
will never do an explicit one.


Lazare INEPOLOGLOU
Ingénieur Logiciel


2012/3/7 Alan Knowles <a...@akbkhome.com>

> Comment in-line below...
>
> On Wednesday, March 07, 2012 07:10 AM, Simon Schick wrote:
>
>> Hi,:
>>
>> It got quite around that because we have some RFCs to this where the
>> functionality seems to be defined as the people thought it should be.
>> Otherwise they can raise their hands and write a mail that they want to
>> update the RFC - but as there's no one doing that, I think we're quite
>> close to what we wanted.
>>
>> Take a look at it and feel free to add your ideas in this thread.
>> https://wiki.php.net/rfc/parameter_type_casting_hints
>> https://wiki.php.net/rfc/object_cast_to_types
>>
>
> Been watching this for a while, got slightly interested in the parameter
> casting thing, but then ran a little test
> http://www.roojs.com/examples/types.php
>
> Casting on PHP may help the code inside the function, but the result is
> some weird and wonderful magic for the calling code, not to mention that
> the docs for (int) casting say the behavior may change, don't expect it to
> work like that forever..
>
> You could try some kind of stricter casting. Where odd conversions between
> array/object <-> bool/int/float/string do not work on parameter casting ..
> but then you end up with two types of casting ...
>
> Anyway, will go back to lurking for a while..
>
> Regards
> Alan
>
>
>> Bye
>> Simon
>>
>> 2012/3/6 Kris Craig<kris.cr...@gmail.com>
>>
>>  Wow no offense, but your timing is terrible, Raymond!  We've been going
>>> back and forth on this for the past couple weeks now, though the
>>> discussion
>>> has quieted for the moment.
>>>
>>> I would suggest you go through some of the recent posts on Internals.
>>> Right now there basically is no solid consensus on this issue, though
>>> some
>>> of us have been working to change that.  But as it stands now, I'm not
>>> aware of any plans to introduce expanded typing of any kind in the
>>> foreseeable future.  And even if we did, I highly doubt it would happen
>>> before PHP 6.
>>>
>>> --Kris
>>>
>>>
>>> On Mon, Mar 5, 2012 at 6:20 PM, Raymond Irving<xwis...@gmail.com>
>>>  wrote:
>>>
>>>  Hello,
>>>>
>>>> I came across some info on the web that states that scalar type hinting
>>>>
>>> was
>>>
>>>> added to the PHP trunk but it did not make it's way into 5.4 because of
>>>> objections from the community. Will it ever make it's way into 5.5?
>>>>
>>>> I know PHP is considered to be a weak typed language but it should also
>>>>
>>> be
>>>
>>>> about freedom. Freedom for a PHP developer to choose to use scalar type
>>>> hinting whenever he/she sees the need.
>>>>
>>>>
>>>> Best regards,
>>>> __
>>>> Raymond
>>>>
>>>>
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

Reply via email to