PHP 检查是否声明了静态类 [英] Php Check If a Static Class is Declared
本文介绍了PHP 检查是否声明了静态类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何检查是否已声明静态类?前任鉴于班级
How can i check to see if a static class has been declared? ex Given the class
class bob {
function yippie() {
echo "skippie";
}
}
稍后在代码中我如何检查:
later in code how do i check:
if(is_a_valid_static_object(bob)) {
bob::yippie();
}
所以我不明白:致命错误:在第 3 行的 file.php 中找不到类bob"
so i don't get: Fatal error: Class 'bob' not found in file.php on line 3
推荐答案
您也可以检查特定方法是否存在,甚至无需实例化类
You can also check for existence of a specific method, even without instantiating the class
echo method_exists( bob, 'yippie' ) ? 'yes' : 'no';
如果您想更进一步并验证yippie"实际上是静态的,请使用 反射 API(仅限 PHP5)
If you want to go one step further and verify that "yippie" is actually static, use the Reflection API (PHP5 only)
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();
}
或者,你可以结合这两种方法
or, you could combine the two approaches
if ( method_exists( bob, 'yippie' ) )
{
$method = new ReflectionMethod( 'bob::yippie' );
if ( $method->isStatic() )
{
// verified that bob::yippie is defined AND static, proceed
}
}
这篇关于PHP 检查是否声明了静态类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文