февруари 17

Class Casting в PHP

Аз също съм се чудил, защо PHP все още няма такава възможност, при която да може да cast-неш един обект в друг. Ето едно интересно решение:

/**
 * 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)
   ));
}

Повече може да прочетете тук:

Няма коментари

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.