PHP: Hypertext Preprocessor (original) (raw)
ReflectionClass::getConstructor
(PHP 5, PHP 7, PHP 8)
ReflectionClass::getConstructor — Gets the constructor of the class
Description
Parameters
This function has no parameters.
Return Values
A ReflectionMethod object reflecting the class' constructor, or [null](reserved.constants.php#constant.null)
if the class has no constructor.
Examples
Example #1 Basic usage of ReflectionClass::getConstructor()
<?php $class = new ReflectionClass('ReflectionClass'); <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">constructor = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6151em;"></span><span class="mord mathnormal">co</span><span class="mord mathnormal">n</span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">u</span><span class="mord mathnormal">c</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">or</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>class->getConstructor(); var_dump($constructor); ?>
The above example will output:
object(ReflectionMethod)#2 (2) { ["name"]=> string(11) "__construct" ["class"]=> string(15) "ReflectionClass" }
Found A Problem?
martin dot melka at gmail dot com ¶
5 years ago
`This method will return the parent's constructor, if the current class does not override it.
NULL will only be returned when the class has no constructor AND none of its parents have a constructor either.
`
14 years ago
`Just posting some example code for anyone wanting to mess around with this stuff:
word="HelloWorld",word = "Hello World", word="HelloWorld",options = array('a', 'b')) { this−>whattosay=this->what_to_say = this−>whattosay=word; } public function speak() { echo $this->what_to_say; } }$class = new ReflectionClass('Say');$constructor = $class->getConstructor(); echo $constructor;/* OUTPUTS: Method [ public method __construct ] { @@ /reflect.php 6 - 9 - Parameters [3] { Parameter #0 [ $no_default ] Parameter #1 [ $word = 'Hello World' ] Parameter #2 [ $options = Array ] } } */ parameters=parameters = 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().param->allowsNull() . 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 */ ?>To clarify the possibly confusing behavior of ReflectionParemeter::isArray(), it will return true if the parameter has type hinting:
word="HelloWorld",arrayword = "Hello World", array word="HelloWorld",arrayoptions = array('a', 'b')) ... ?>Calling isArray() will now return true for the $options parameter
`
13 years ago
`Old constructors also count as contructor:
refl=newReflectionClass(′SomeClass′);vardump(refl = new ReflectionClass('SomeClass');var_dump(refl=newReflectionClass(′SomeClass′);vardump(refl->isInstantiable()); // bool(true)echo $refl->getConstructor();/* OUTPUT: Method [ public method SomeClass ] { @@ /var/www/vhosts/api.example.com/httpdocs/testRefl.php 5 - 6 - Parameters [1] { Parameter #0 [ $some_arg ] } } */ ?>Some more behavior:
refl=newReflectionClass(′SomeClass′);vardump(refl = new ReflectionClass('SomeClass');var_dump(refl=newReflectionClass(′SomeClass′);vardump(refl->isInstantiable()); // bool(true)var_dump($refl->getConstructor()); // NULL /* --------------- */ class AnotherClass { private function __construct() { } function funcB($arg1, $arg2) { } } refl=newReflectionClass(′AnotherClass′);vardump(refl = new ReflectionClass('AnotherClass');var_dump(refl=newReflectionClass(′AnotherClass′);vardump(refl->isInstantiable()); // bool(false)echo $refl->getConstructor(); /* Method [ private method __construct ] { @@ /testRefl.php 22 - 23 } */?>Tested on PHP 5.2.4
`