Class cast

php.internals

David Rodrigues

6 years ago
Currently PHP supports generic castings like (string), (int), ... maybe is time to allow class castings like (ToClass) $fromObject? I think that it could be useful to converts to another kind of structure, or even to reduce object type. For instance: --- 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. --- 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. --- To make possible custom some kind of castings, I suggests a new magic method __cast($value). class Number { ... function __cast($value) { if (is_int($value) || is_float($value)) return new static($value); throw new TypeError(); } } Just to talk. Atenciosamente, David Rodrigues

Benjamin Morel

6 years ago
> > Currently PHP supports generic castings like (string), (int), ... maybe is > time to allow class castings like (ToClass) $fromObject?
I've proposed something similar a year ago: https://externals.io/message/105332 My intention wasn't to create an object from a scalar, nor was it to limit to a class' methods & properties (hardly possible in PHP), but to ensure that the value is an instance of the given class/interface, or throw an exception. My proposal wasn't so well received, though. — Ben

Stas Malyshev

6 years ago
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 smalyshev@gmail.com

David Rodrigues

6 years ago
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 < smalyshev@gmail.com> escreveu:

Stas Malyshev

6 years ago
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