Hello!

> What's wrong with new Number(123)?

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.

> Not sure why would you want to do this? What's the use case?

You could mix the previous answer when arguments to a method that expects a
Number, for instance: X::test((Number) $x).

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);
}

Then use:

requiresB((B) $a);



Atenciosamente,
David Rodrigues


Em qua., 25 de mar. de 2020 às 20:06, Stanislav Malyshev <
smalys...@gmail.com> escreveu:

> Hi!
>
> > 1. Converting from A to B:
> >
> > $a = 123;
> > $b = (Number) $a; // $b is now instanceof Number
> >
> > A instanceof Number will created based on $a value.
>
> What's wrong with new Number(123)?
>
> > 2. Reduce object type (I don't know the technical term):
> >
> > class A {}
> > class B extends A {}
> >
> > $b = new B;
> > $a = (A) $b;
> >
> > $a still is $b, but with "limited" access to A methods and properties.
>
> Not sure why would you want to do this? What's the use case?
> --
> Stas Malyshev
> smalys...@gmail.com
>

Reply via email to