Re: Class cast
| From: | Stanislav Malyshev | Date: | Thu, 26 Mar 2020 06:20:09 +0000 |
| Subject: | Re: Class cast | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-109322@lists.php.net to get a copy of this message | ||
Hi!
> Actually it is a simplest case, where only a single information was
> considered. But you can expand it to something more complex, where
> currently you will need create a static method to copy the data, which
> is not a normalized way like cast could do.
You can always use anonymous functions if callable is needed.
>
> Or you can increase the type of information when possible. For instance:
>
> function requiresB(B $b);
>
> But you have only A $a.
>
> You can create:
>
> function __cast($value) {
> if ($value instanceof A) return B($value, 123, M_PI);
> }
That's not a cast. That's creating new object from another object.
Things like this should be an explicit call. Otherwise the meaning of
this code is unclear - it pretends that you just change the type, but in
fact it's a completely new object which can be entirely unrelated to the
old one. Things like that should be explicit.
--
Stas Malyshev
smalyshev@gmail.com