Yii Framework Forum: Object Casting - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Object Casting Rate Topic: -----

#1 User is offline   Milo Schuman 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 20
  • Joined: 17-September 10

Posted 07 October 2010 - 08:46 AM

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.
0

#2 User is offline   kpapageorgiou 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 27-February 14

Posted 22 May 2014 - 11:52 AM

U are a little God my friend, this saved me
Thank you
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users