php function foo(array $arg = NULL) -- why the array and NULL?
I've see开发者_开发百科n the following a few times lately:
function foo(array $arg = NULL) { ... }
My question being why make the default of $arg
NULL
when it will be just cast into an array? Why not do:
function foo(array $arg = array()) { ... }
I know it doesn't really make much difference--it's mostly just reading code--but why encourage PHP to be changing data types all the time.
I've seen this a lot in Kohana.
The real question is why create an array when you don't need one.
If you use $arg = array(), there will be a specific instruction to create an array, even if it's PHP an instruction still consumes CPU cycles. If you just do $arg = NULL, then nothing will be done.
My guess is that they want an array for a parameter, but a NULL
value for the parameter is acceptible (and will be used by default if an array -- or an explicit NULL
-- isn't given).
Note that the array
specification in the function signature is not about casting, but is a type hint. It says that only arrays are accepted for $arg
.
=NULL
is a way to allow the argument to have a value of NULL:
function foo(array $arg = NULL) { ... }
allows the execution of foo($bar);
in the case that $bar === NULL
.
function foo(array $arg = array()) { ... }
will trigger an error if $bar === NULL
, since NULL is not of hinted type, array.
Take a look at the PHP type hinting page.
This is useful if:
$bar = NULL;
// Do stuff that may involve $bar
// $bar may be an array, or it may still be NULL
foo($bar);
// For the above line to not trigger an error if $bar === NULL, you must use
function foo(array $arg = NULL) { ... }
You get this error unless you do this.
The reason is probably to distinguish between an empty set of objects to process, and slightly different functionality when nothing is put in.
As a hypothetical example, let's say you have a function that creates reports for users. The input array contains the IDs (or even objects) of the users for which the reports are to be generated. The same function might as well be used to process the reports when you need to process all of the users, as opposed to a certain set of them. When you only want to process for specific users, you would throw in the array. But if you wanted to process all of them, then it makes sense that the parameter is a distinct NULL instead of a "no users", which would be an empty array.
For example, let's say that there's a page where an administrator can designate which users to create reports for. But the admin presses the "Create Reports" button without selecting any users, in which case an empty array would get thrown into the function, and the function would accurately process no users, because there are no users in the array.
Then perhaps you might have a different button somewhere else in this hypothetical system, "Create All Reports", in which case you wouldn't throw anything in, and the function would be able to tell the difference between "number of users = 0" and "users not provided", which in this case would mean "all users".
That's one example. In general, I use NULL as a default parameter to be able to distinguish within the function whether something was passed or not, because the behavior of the function might be different when nothing in particular is specified, as per the example given above.
This is a couple of years old I know, but I can't help but say the wrong answer was chosen and nobody clearly answered the full question. So for others that are looking at this and want to know what's really going on here:
function foo(array $arg = NULL) { ... }
When you call foo() the first thing that will happen is PHP will test $arg to see if it's an array. That's right, array $arg is a test. If $arg is not an array you will see this error:
If $arg is a string:
Catchable fatal error: Argument 1 passed to foo() must be an array, string given
Ok, then NULL just means that in case $arg is not passed we will set it to NULL. It's just a way to make $arg optional, or preset its value.
And just to be clear, there's no such thing as type casting function parameters without using the RFC patch, here.
If you use the RFC patch you can do:
function foo((array)$arg){ ... }
maybe if your foo function has to do something when the function is called explicitly with an empty array.. i mean:
if
foo(array());
doesn't do the same thing than
foo();
then you'd have to use the $arg = NULL
精彩评论