This question is related to downcasting in php5
How expensive is this php downcasting workaround?
Is this php downcasting workaround too expensive?
I've echoed microtimes and It seems that it takes like 0.001. I wonder if It could be a problem in a large foreach.
public static function to($obj) {
return unserialize(preg_replace(
'/^O:\d+:\"(\w+)/',
'O:' . strlen('this_class_name') . ':"' . 'this_class_name',
serialize($obj)));
}
Solutions proposed in http://php.net/manual/en/language.types.type-juggling.php comments are similar.