
Untitled
By: a guest on
Jul 17th, 2012 | syntax:
None | size: 0.94 KB | hits: 13 | expires: Never
How do I run code based on the true class name of an object?
if($myobject instanceof class1){
}elseif($myobject instanceof class1){
}
switch(get_class($myobject)){
case 'class1':
break;
case 'class2':
break;
}
function objectProcessor_A($o) {}
function objectProcessor_B($o) {}
function objectProcessor_C($o) {}
foreach($objects AS $o) {
if(function_exist("objectProcessor_" . get_class($o)))
call_user_func("objectProcessor_" . get_class($o), $o);
}
class OFactory {
static function getO($type) {
$r = new $type();
if($type === "A") {
$r.processor = function() {...};
} else if($type === "B") {
$r.processor = function() {...};
} else if($type === "C") {
$r.processor = function() {...};
}
return $r;
}
}
$a = OFactory::getO("A");
call_user_func($a->processor);
$b = OFactory::getO("B");
call_user_func($b->processor);