On Sat, Apr 2, 2011 at 12:41 PM, Stan Vass <sv_for...@fmethod.com> wrote:

> This is actually something I have been toying with a bit. Adding the
>> ability to call methods on both strings and arrays. I still don't like
>> the idea of making them real objects as the overhead and the amount of
>> code that would need to be changed in the core and in every extension is
>> daunting. Anybody out there have a patch along these lines?
>>
>> -Rasmus
>>
>
>
> Glad to see pseudo-object methods for scalars/arrays brought up again.
> It'll improve workflow a lot. I want to ask/suggest two clarifications on
> this:
>
> 1) You said 'strings and arrays', I hope that's 'scalars and arrays'. As
> the current PHP semantics work, and with very minor exceptions, int/float
> works as a numeric string when passed to a string function, and numeric
> strings work when passed to a number function.
>
> I.e. those should work:
>
> $a = 1234; /* int */ echo $a->substr(1); // '234'
> $a = '1.23456'; /* string */ $a->format(2); // '1.23'
>
> This means the extension methods for primitives should be split in two
> groups:
>
> arrays (lists and maps) - one API set
> scalars (bool, int, float, strings) - one API set
>
> 2) Because the language currently has no scalar hinting (I know it's
> coming), IDE autocompletion of the extension methods may not work very well
> (unless the IDE assumes everything is a scalar unless hinted otherwise).
>
> Both hinting, incl. for method/function return types, and extension methods
> should be introduced together as they make each other more useful, than
> independently.
>
> Let me know what you think.
>
> Stan Vass
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
What about a marker interface hierarchy like this?
interface Mixed { }
interface Numeric { }

interface Callable  extends Mixed { }
interface Resource  extends Mixed { }
interface Scalar    extends Mixed { }
interface Array     extends Mixed { } // unexpected T_ARRAY
interface Object    extends Mixed { }

interface Boolean   extends Scalar { }
interface String    extends Scalar, Numeric { }
interface Number    extends Scalar, Numeric { }

interface Float     extends Number { }
interface Integer   extends Number { }

( http://graph.gafol.net/etSevVPSJ )

This way the core will know which methods are allowed to call on each $var

Advantages:
* cannnot create instances
* cannot extends these interfaces (private to core)
* very very fine type hint
* core implementation is not exposed


 Martin Scotta

Reply via email to