Here's an interesting situation I came across lately, that I haven't found an answer to yet.
I have 2 objects which both inherit from the same abstract parent. Let's call the parent a "vehicle" and the 2 children "car" and "truck". Then let's say a script creates a new instance of one of the vehicle types. It does this by calling a Factory class which creates the instance of the actual type of vehicle subclass that is appropriate. The calling script then runs methods, changes properties, etc on the $vehicle object. It doesn't know, nor care, what type of vehicle object it is, as all the vehicle objects implement the same methods and have the same properties- the only difference is the actual insides of these methods.
Let's also say that the actual object type returned by the Factory was a car object. The calling script (or even the car class itself) has modified it to the point where it no longer "qualifies" to be a car object, it should be changed to a truck object. Let's say the payload was increased over the threshold for a car. What I would like to happen at this point is for the car class to detect this and "morph" into a truck object. This should be transparent to the calling script. At worst, the car class would throw an exception to indicate to the caller that it has been changed.
Such a thing does not appear to be possible in PHP, at least to the level of transparancy that I would like. I tried stupid yet obvious stuff like when the car class detects this it creates a new truck object, copies all its properties to it, then replaces $this with $truck. Compiler doesn't like that syntax. Is such a thing possible in other OO languages? Is there a term for what I am describing?
Now, my solution is to be something like throwing an exception to the caller indiciating what class the current one should be converted to, then providing methods for doing the conversion (i.e. car would have a toTruck() method which would return a new Truck object with all of the car's properties copied over).