Fwd: [PHP-DEV] Re: [Concept] Magic Casting
| From: | Alex Bowers | Date: | Fri, 02 Dec 2016 14:56:49 +0000 |
| Subject: | Fwd: [PHP-DEV] Re: [Concept] Magic Casting | ||
| References: | 1 2 3 4 5 6 7 8 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-97263@lists.php.net to get a copy of this message | ||
Sorry for forward. Hit reply, not reply-all.
---------- Forwarded message ----------
From: Alex Bowers <bowersbros@gmail.com>
Date: 2 December 2016 at 14:31
Subject: Re: [PHP-DEV] Re: [Concept] Magic Casting
To: David Rodrigues <david.proweb@gmail.com>
Another benefit this would give frameworks / user land code is the ability
to mock / simulate having scalar object types.
For instance, A class called
Str could be used to cast all "string"
inputs to its type, and then everywhere in the framework / user land code,
based on __cast() it could cast them all to a Str instance, and be used
with methods on.
This will allow
$string->toUpperCase() to be possible etc, whilst acting on a plain string.
On 2 December 2016 at 14:30, Alex Bowers <bowersbros@gmail.com> wrote:
> To me, in pseudo logic it would work as so:
>
> Currently it does this:
>
> A method with the signature of function_name(Collection $collection) has
> been called. The parameter I have is of type Collection. Let it continue;
> or alternatively, the parameter is not of type Collection, throw the error.
>
> The new method will be:
>
> A method with the signature of function_name(Collection $collection) has
> been called. The parameter I have is of type collection. let it continue.
>
> Alternatively,
> The parameter is not of type collection, does Collection implement a
> __cast() magic method. If it does, Pass the parameter into that __cast()
> method. If the response is an instance of Collection, let it through. If it
> is not, throw the error.
>
> As to how this would be implemented, r how the engine is built. This is
> not something I am aware of really. I will be willing to learn, and assist
> somebody / get assistance in the building of this feature, but it would
> take me a lot longer than it would take a real core member to do.
>
> But effectively, it will call Type::__cast()
>