ReflectionClass
在线手册:中文 英文
PHP手册

ReflectionClass::hasMethod

(PHP 5 >= 5.1.0)

ReflectionClass::hasMethodChecks if method is defined

说明

public bool ReflectionClass::hasMethod ( string $name )

Checks whether a specific method is defined in a class.

参数

name

Name of the method being checked for.

返回值

TRUE if it has the method, otherwise FALSE

范例

Example #1 ReflectionClass::hasMethod() example

<?php
Class {
    public function 
publicFoo() {
        return 
true;
    }

    protected function 
protectedFoo() {
        return 
true;
    }

    private function 
privateFoo() {
        return 
true;
    }

    static function 
staticFoo() {
        return 
true;
    }
}

$rc = new ReflectionClass("C");

var_dump($rc->hasMethod('publicFoo'));

var_dump($rc->hasMethod('protectedFoo'));

var_dump($rc->hasMethod('privateFoo'));

var_dump($rc->hasMethod('staticFoo'));

// C should not have method bar
var_dump($rc->hasMethod('bar'));

// Method names are case insensitive
var_dump($rc->hasMethod('PUBLICfOO'));
?>

以上例程会输出:

bool(true)
bool(true)
bool(true)
bool(true)
bool(false)
bool(true)

参见


ReflectionClass
在线手册:中文 英文
PHP手册
PHP手册 - N: Checks if method is defined

用户评论:

phoenix at todofixthis dot com (28-Oct-2010 05:47)

Parent methods (regardless of visibility) are also available to a ReflectionObject.  E.g.,

<?php
class ParentObject {
  public function
parentPublic(  ) {
  }

  private function
parentPrivate(  ) {
  }
}

class
ChildObject extends ParentObject {
}

$Instance = new ChildObject();
$Reflector = new ReflectionObject($Instance);

var_dump($Reflector->hasMethod('parentPublic'));  // true
var_dump($Reflector->hasMethod('parentPrivate')); // true
?>

hanguofeng at gmail dot com (20-Oct-2010 05:09)

note that even if private method will also be 'has'.