2024 日本 PHP 研討會

ReflectionClass::getConstructor

(PHP 5, PHP 7, PHP 8)

ReflectionClass::getConstructor取得類別的建構子

說明

public ReflectionClass::getConstructor(): ?ReflectionMethod

取得反射類別的建構子。

參數

此函式沒有參數。

回傳值

一個 ReflectionMethod 物件,反映類別的建構子;如果類別沒有建構子,則回傳 null

範例

範例 #1 ReflectionClass::getConstructor() 的基本用法

<?php
$class
= new ReflectionClass('ReflectionClass');
$constructor = $class->getConstructor();
var_dump($constructor);
?>

上述範例將輸出

object(ReflectionMethod)#2 (2) {
  ["name"]=>
  string(11) "__construct"
  ["class"]=>
  string(15) "ReflectionClass"
}

參見

新增註記

使用者貢獻的註記 3 則註記

martin dot melka at gmail dot com
5 年前
如果目前的類別沒有覆寫父類別的建構子,則此方法將會回傳父類別的建構子。

只有當類別本身沒有建構子,且其所有父類別也都沒有建構子的情況下,才會回傳 NULL。
Rob McVey
14 年前
提供一些範例程式碼給想研究這方面的人

<?php

class Say
{
private
$what_to_say;
public function
__construct($no_default, $word = "Hello World", $options = array('a', 'b'))
{
$this->what_to_say = $word;
}

public function
speak()
{
echo
$this->what_to_say;
}
}

$class = new ReflectionClass('Say');

$constructor = $class->getConstructor();

echo
$constructor;

/* OUTPUTS:

Method [ <user, ctor> public method __construct ] {
@@ /reflect.php 6 - 9

- Parameters [3] {
Parameter #0 [ <required> $no_default ]
Parameter #1 [ <optional> $word = 'Hello World' ]
Parameter #2 [ <optional> $options = Array ]
}
}

*/

$parameters = $constructor->getParameters();

var_export($parameters);

/* OUTPUT:

array (
0 =>
ReflectionParameter::__set_state(array(
'name' => 'no_default',
)),
1 =>
ReflectionParameter::__set_state(array(
'name' => 'word',
)),
2 =>
ReflectionParameter::__set_state(array(
'name' => 'options',
)),
)

*/

$nl = "\n";
echo
"$nl\tParameters$nl";
foreach(
$parameters as $param)
{
echo
"****** $" . $param->name . " ******$nl";
echo
"Nullable:\t\t" . $param->allowsNull() . $nl
."Default Value:\t\t";
echo (
$param->isDefaultValueAvailable()) ? $param->getDefaultValue() : "None";
echo
$nl ."Is Array:\t\t";
echo (
$param->isArray()) ? "Yes" : "No";
echo
$nl . "Optional:\t\t";
echo (
$param->isOptional()) ? "Yes" : "No";
echo
$nl;
}

/* OUTPUT:

Parameters
****** $no_default ******
Nullable: 1
Default Value: None
Is Array: No
Optional: No
****** $word ******
Nullable: 1
Default Value: Hello World
Is Array: No
Optional: Yes
****** $options ******
Nullable: 1
Default Value: Array
Is Array: No
Optional: Yes

*/

?>

為了釐清 ReflectionParemeter::isArray() 可能令人困惑的行為,如果參數有類型提示,它將會回傳 true

<?php
...
public function
__construct($no_default, $word = "Hello World", array $options = array('a', 'b'))
...
?>

呼叫 isArray() 現在將會針對 $options 參數回傳 true
jochem at drecomm dot nl
13 年前
舊式建構子也算建構子

<?php

類別 SomeClass {

函數
SomeClass($some_arg) {
}

}

$refl = new ReflectionClass('SomeClass');

var_dump($refl->isInstantiable()); // bool(true)

echo $refl->getConstructor();

/* 輸出:
方法 [ <user, ctor> 公開方法 SomeClass ] {
@@ /var/www/vhosts/api.example.com/httpdocs/testRefl.php 5 - 6

- 參數 [1] {
參數 #0 [ <required> $some_arg ]
}
}
*/

?>

其他行為

<?php

類別 SomeClass {

函數
funcA($arg1, $arg2) {

}

}

$refl = new ReflectionClass('SomeClass');

var_dump($refl->isInstantiable()); // bool(true)

var_dump($refl->getConstructor()); // NULL

/* --------------- */

類別 AnotherClass {

私有 函數
__construct() {
}

函數
funcB($arg1, $arg2) {

}

}

$refl = new ReflectionClass('AnotherClass');

var_dump($refl->isInstantiable()); // bool(false)

echo $refl->getConstructor();
/*
方法 [ <user, ctor> 私有方法 __construct ] {
@@ /testRefl.php 22 - 23
}
*/

?>

在 PHP 5.2.4 上測試
To Top