Skip to content

Instantly share code, notes, and snippets.

@kensnyder
Created November 13, 2013 21:13
Show Gist options
  • Save kensnyder/7456505 to your computer and use it in GitHub Desktop.
Save kensnyder/7456505 to your computer and use it in GitHub Desktop.
<?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