php – 检查扩展类中是否存在方法,但不检查父类
发布时间:2020-05-26 02:13:15 所属栏目:PHP 来源:互联网
导读:使用method_exists,它会检查所有方法,包括父类. 例: class Toot { function Good() {}}class Tootsie extends Toot { function Bad() {}}function testMethodExists() { // true var_dump(method_exists(Too
|
使用method_exists,它会检查所有方法,包括父类. 例: class Toot {
function Good() {}
}
class Tootsie extends Toot {
function Bad() {}
}
function testMethodExists() {
// true
var_dump(method_exists('Toot','Good'));
// false
var_dump(method_exists('Toot','Bad'));
// true
var_dump(method_exists('Tootsie','Good'));
// true
var_dump(method_exists('Tootsie','Bad'));
}
如何检查该方法仅存在于当前类而不是父类(即Tootsie)? 由于v.4.0.5 php有 get_parent_class()方法,因此返回父类.所以你可以在没有重新选择的情况下处理它:class A
{
function a() { /* ... * |
