如何检查静态类是否已声明?
前任
鉴于班级
class bob {
function yippie() {
echo "skippie";
}
}
稍后在代码中我如何检查:
if(is_a_valid_static_object(bob)) {
bob::yippie();
}
所以我不明白:
致命错误:在 file.php 第 3 行中找不到类“bob”
即使没有实例化该类,您也可以检查特定方法是否存在
echo method_exists( bob, 'yippie' ) ? 'yes' : 'no';
如果您想更进一步并验证“yippie”实际上是静态的,请使用反射API(仅限 PHP5)
try {
$method = new ReflectionMethod( 'bob::yippie' );
if ( $method->isStatic() )
{
// verified that bob::yippie is defined AND static, proceed
}
}
catch ( ReflectionException $e )
{
// method does not exist
echo $e->getMessage();
}
或者,您可以结合这两种方法
if ( method_exists( bob, 'yippie' ) )
{
$method = new ReflectionMethod( 'bob::yippie' );
if ( $method->isStatic() )
{
// verified that bob::yippie is defined AND static, proceed
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)