Re: [PHP-DEV] Type casting syntax

This is only part of a thread. view whole thread
  115393
July 12, 2021 01:38 mike@newclarity.net (Mike Schinkel)
> On Jul 11, 2021, at 7:19 PM, Larry Garfield <larry@garfieldtech.com> wrote: > > What are the use cases for integrated object to object (ie, class to class) conversion in PHP? > I'm not entirely clear on what the use case is in PHP. When would that be superior to "just write an asFoo() method and move on with life"?
Reading your question triggered the following thoughts. They are not use-case's per-se because I can't remember them, but I know I have felt the need for these each at least once, and especially the last one. Note that you cannot currently do any of these with an asFoo() method, at least not directly. 1. It would be nice to be able to create a new instance of a parent, grandparent class, etc. class given the properties of a child class instance. Maybe: class Progenitor { public $surname; function __construct($surname) { $this-> surname = $surname; } } class Offspring extends Progenitor {} $offspring = new Offspring('Schinkel'); $progenitor = clone $offspring asinstanceof Progenitor echo get_class($progenitor); // prints: Progenitor echo $progenitor->surname; // prints: Schinkel 2. Similarly it would be nice to be able to treat an object as an instance of its parent/grandparent/etc. where (parentof $instance) === $instance $progenitor = $offspring asinstanceof Progenitor echo get_class($progenitor); // prints: Progenitor echo $progenitor === $offspring ? 'frue' : 'false; // prints: true Note I have no idea if having two instances where references are equal but they are different classes would have unintended consequences, so this might not be a good idea, not sure. But it would be nice to be able to treat an object as its parent from time to time, if it is possible. 3. It would also be nice to create a child class starting with an instance of a parent as its base. See example for #5. It should work the same. 4. Similarly it might be nice to be able to (somehow) assign a child class' identity and properties to an instance of its parent/grandparent/etc. where (childof $instance) === $instance although I have absolutely no idea how it this would work syntactically. ??? 5. And probably the functionality I've wanted most in this area — which is admittedly only tangentially-related — is to be able to assign to $this so we could do something like clone an object to get an equivalent object. Or if case #1 or #2 above was possible, by cloning an instance of its parent/grandparent/etc. would initialize for that object. For example: class Foo { public $value; function __construct(int $value) { $this->value = $value; } function fromFoo(Foo $foo) { $this = clone $foo; // <-- assigning $this replaces all state to be equal to the cloned instance } } $f1 = new Foo(100); $f2 = new Foo(0); $f2->fromFoo($f1); echo $f2->value; // prints 100
> Scalar conversion I can see, but every time someone suggests adding siblings to __toString(), there's major pushback.
Look ma, no magic methods! :-) -Mike