ReflectionClass::getConstructor()函数是PHP中的内置函数,用于返回指定类的构造函数,如果该类没有任何构造函数,则返回NULL。
用法:
ReflectionMethod ReflectionClass::getConstructor( void )
参数:该函数不接受任何参数。
返回值:该函数返回指定类的构造函数,如果该类没有任何构造函数,则返回NULL。
以下示例程序旨在说明PHP中的ReflectionClass::getConstructor()函数:
示例1:
// Using ReflectionClass over the class named as ReflectionClass
$Class = new ReflectionClass('ReflectionClass');
// Calling the getConstructor() function
$constructor = $Class->getConstructor();
// Getting the constructor for the defined Class
var_dump($constructor);
?>
输出:
object(ReflectionMethod)#2 (2) {
["name"]=>
string(11) "__construct"
["class"]=>
string(15) "ReflectionClass"
}
示例2:
// Defining a user-defined class Company
class Company {
public function GeeksforGeeks() { }
static function gfg() { }
}
// Using ReflectionClass over the class Company
$A = new ReflectionClass("Company");
// Calling the getConstructor() function
$B = $A->getConstructor();
// Getting the constructor for the defined Class
// or NULL if the constructor is not present
var_dump($B);
?>
输出:
NULL