Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
$foo = castObject($bar, '\\My\\FooObject');
function castObject($obj, $toClass) {
if (is_callable("$toClass::__cast")) {
return call_user_func("$toClass::__cast", $obj);
}
if ($obj instanceof $toClass) {
return $obj;
}
// some default behavior here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment