We have a system where we have reason to instantiate an object before we know what its specific type is. For example, we wish to instantiate a class "media" before we know whether the final class will be "book" or "cd".
Here is what we are doing. We instantiate the "media" object, and once we know what type of media it is, we instantiate a "book", passing in the media object.
class Book extends Media
{
public function __construct( $parent )
{
$vars = get_object_vars( $parent );
foreach( $vars as $key => $value )
$this->$key = $value;
}
}
//elsewhere
$item = new Media();
$item->setPrice( $price );
//other code, figure out the item type
$item = new Book( $item );
Is there a better way to do something like this? Is this dynamic polymorphism?