note 98937 added to reflectionclass.getconstructor
| From: | RobMcVey at osu1 dot php dot net | Date: | Fri, 16 Jul 2010 14:47:34 +0000 |
| Subject: | note 98937 added to reflectionclass.getconstructor | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-170913@lists.php.net to get a copy of this message | ||
Just posting some example code for anyone wanting to mess around with this stuff:
<?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
*/
?>
To clarify the possibly confusing behavior of ReflectionParemeter::isArray(), it will return true if
the parameter has type hinting:
<?php
...
public function __construct($no_default, $word = "Hello World", array $options =
array('a', 'b'))
...
?>
Calling isArray() will now return true for the $options parameter
----
Server IP: 69.147.83.197
Probable Submitter: 68.188.12.130
----
Manual Page -- http://www.php.net/manual/en/reflectionclass.getconstructor.php
Edit -- https://master.php.net/note/edit/98937
Del: integrated -- https://master.php.net/note/delete/98937/integrated
Del: useless -- https://master.php.net/note/delete/98937/useless
Del: bad code -- https://master.php.net/note/delete/98937/bad+code
Del: spam -- https://master.php.net/note/delete/98937/spam
Del: non-english -- https://master.php.net/note/delete/98937/non-english
Del: in docs -- https://master.php.net/note/delete/98937/in+docs
Del: other reasons-- https://master.php.net/note/delete/98937
Reject -- https://master.php.net/note/reject/98937
Search -- https://master.php.net/manage/user-notes.php