php中constructor,PHP ReflectionClass getConstructor()用法及代码示例

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值