For anyone looking for a way to cast an object of type ClassX into an object of type ClassY, I found this solution on a blog post by Arnold Daniels: A Dark Corner of PHP: Class Casting.
/**
* Cast an object to another class, keeping the properties, but changing the methods
*
* @param string $class Class name
* @param object $object
* @return object
*/
function casttoclass($class, $object)
{
return unserialize(preg_replace('/^O:\d+:"[^"]++"/', 'O:' . strlen($class) . ':"' . $class . '"', serialize($object)));
}
I am interested if anyone has a better, alternative, or more Yii-centric solution or other insights.