需要php5.3静态继承的帮助吗
PHP5.3中的这种静态“继承”有点问题吗 我需要测试静态类中是否存在静态函数,但我需要从父静态类中测试它 我知道在PHP5.3中,我可以使用'static'关键字来模拟'this'关键字。 我只是找不到一种方法来测试函数是否存在 以下是一个例子:需要php5.3静态继承的帮助吗,php,function,static,php-5.3,Php,Function,Static,Php 5.3,PHP5.3中的这种静态“继承”有点问题吗 我需要测试静态类中是否存在静态函数,但我需要从父静态类中测试它 我知道在PHP5.3中,我可以使用'static'关键字来模拟'this'关键字。 我只是找不到一种方法来测试函数是否存在 以下是一个例子: // parent class class A{ // class B will be extending it and may or may not have // static function name 'func' // i need t
// parent class
class A{
// class B will be extending it and may or may not have
// static function name 'func'
// i need to test for it
public static function parse(array $a){
if(function_exists(array(static, 'func'){
static::func($a);
}
}
}
class B extends A {
public static function func( array $a ){
// does something
}
}
所以现在我需要执行B::parse()代码>
这个想法是,如果子类有一个函数,它将被使用,
否则将不使用
我试过:
function_exists(static::func){}
isset(static::func){}
这两个不起作用
有什么办法吗?
顺便说一句,我知道传递lambda函数作为解决方法的可能性,这不是一个简单的方法
在我的情况下,选择权
我觉得有一个非常简单的解决方案,我现在想不起来
现在我需要打电话试试
public static function parse(array $a){
if(function_exists(array(get_called_class(), 'func') {
/*...*/
请参见对于类和对象(方法),您不能使用函数,只能使用函数。您必须使用方法\u exists
或是可调用的
<代码>isset
仅适用于变量。而且,静态
不模拟$this
,它们是两个完全不同的东西
也就是说,在这种特定情况下,您必须使用带引号的static
关键字的is_callable
:
if (is_callable(array('static', 'func'))) {
static::func();
}
或者
_可调用(数组('static','func'))工作正常。我担心的是,如果“func”不存在,那么is_callable可能至少会发出一个警告:变量没有定义,但它没有定义。如果func函数不存在,则返回false,这是好的。
if (is_callable('static::func')) {
static::func();
}