PHP 父类静态方法中调用子类静态方法
get_called_class() 获取调用该方法的类,如果是在子类中调用父类方法,则返回子类的类名。
或者用static关键字:static::class
如果有多个子类,要在父类中调用子类方法,下面代码可实现:
classparent{abstractprotectedstaticfunctiongetFunc($action);publicstaticfunctioncall($action){//调用子类静态方法,唯一的方式$func=static::getFunc($action);if(!$func){exit();}try{$reflectionMethod=newReflectionMethod(get_called_class(),$func);}catch(ReflectionException$e){exit();}return$reflectionMethod->invoke(null/*,$paras*/);}}classsonextendsparent{publicstaticfunctiongetFunc($action){return"hello";}}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。