Skip to content

Instantly share code, notes, and snippets.

@jk
Last active August 29, 2015 14:13
Show Gist options
  • Save jk/9a3446bfac1072c9521b to your computer and use it in GitHub Desktop.
Save jk/9a3446bfac1072c9521b to your computer and use it in GitHub Desktop.
Get parameter class type hint at runtime
<?php
class Language
{
public function getDefaultLanguage()
{
return "en-US";
}
}
class TestClass
{
public function noLanguage($param1)
{
return $param1;
}
public function withLanguage($param1, Language $language) {
return [
$param1,
$language
];
}
}
$r = new ReflectionClass('TestClass');
$m = $r->getMethods(ReflectionMethod::IS_STATIC | ReflectionMethod::IS_PUBLIC);
foreach ($m as $method) {
$p = $method->getParameters();
foreach ($p as $parameter) {
$c = $parameter->getClass();
var_dump([
'param' => $parameter->name,
'class' => $c->name,
'position' => $parameter->getPosition()
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment