note 104404 added to functions.arguments
| From: | jacob at jacobweber dot com | Date: | Mon, 13 Jun 2011 21:05:56 +0000 |
| Subject: | note 104404 added to functions.arguments | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-180498@lists.php.net to get a copy of this message | ||
This page states:
"Note that when using default arguments, any defaults should be on the right side of any
non-default arguments; otherwise, things will not work as expected."
There seems to be one exception to this. Say you're using type-hinting for an argument, but you
want to allow it to be NULL, and you want additional required arguments to the right of it. PHP
allows this, as long as you give it the type-hinted argument a default value of NULL. For example:
<?php
function sample(ClassA $a = NULL, $b) {
}
sample(new ClassA(), ''); // success
sample(new ClassB(), ''); // failure; wrong type
sample(NULL, ''); // success
sample(new ClassA()); // failure; missing second argument
?>
----
Server IP: 69.147.83.197
Probable Submitter: 24.6.209.102
----
Manual Page -- http://www.php.net/manual/en/functions.arguments.php
Edit -- https://master.php.net/note/edit/104404
Del: integrated -- https://master.php.net/note/delete/104404/integrated
Del: useless -- https://master.php.net/note/delete/104404/useless
Del: bad code -- https://master.php.net/note/delete/104404/bad+code
Del: spam -- https://master.php.net/note/delete/104404/spam
Del: non-english -- https://master.php.net/note/delete/104404/non-english
Del: in docs -- https://master.php.net/note/delete/104404/in+docs
Del: other reasons-- https://master.php.net/note/delete/104404
Reject -- https://master.php.net/note/reject/104404
Search -- https://master.php.net/manage/user-notes.php