note 110182 added to functions.arguments
| From: | Cyril | Date: | Thu, 27 Sep 2012 13:30:24 +0000 |
| Subject: | note 110182 added to functions.arguments | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-191043@lists.php.net to get a copy of this message | ||
@keuleu
@jcaplan
You can use reflexion to resolve your problem:
<?php
class meuh {
function prout($foo = 'hello world'){
$r = new ReflectionObject($this);
foreach ($r->getMethod(__FUNCTION__)->getParameters() as $p){
if($p->isOptional() && is_null(${$p->name})){
${$p->name} = $p->getDefaultValue();
}
}
return $foo;
}
}
$m = new meuh();
var_dump($m->prout('blah')); // display: string 'blah' (length=4)
var_dump($m->prout(null)); // display: string 'hello world' (length=11)
?>
Optional NULL args will be redefined by default value.
----
Server IP: 69.147.83.199
Probable Submitter: 94.228.180.154
----
Manual Page -- http://www.php.net/manual/en/functions.arguments.php
Edit -- https://master.php.net/note/edit/110182
Del: integrated -- https://master.php.net/note/delete/110182/integrated
Del: useless -- https://master.php.net/note/delete/110182/useless
Del: bad code -- https://master.php.net/note/delete/110182/bad+code
Del: spam -- https://master.php.net/note/delete/110182/spam
Del: non-english -- https://master.php.net/note/delete/110182/non-english
Del: in docs -- https://master.php.net/note/delete/110182/in+docs
Del: other reasons-- https://master.php.net/note/delete/110182
Reject -- https://master.php.net/note/reject/110182
Search -- https://master.php.net/manage/user-notes.php