Re: [PHP-DEV] Class cast

This is only part of a thread. view whole thread
  109322
March 26, 2020 06:20 smalyshev@gmail.com (Stanislav Malyshev)
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