views:

114

answers:

5

I've seen 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.

+3  A: 

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.

Daniel Vandersluis
No. Both `= NULL` and `= array()` allow for no arguments, but only `=NULL` allows for the argument to be `NULL`.
Peter Ajtai
+3  A: 

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.

Colin Hebert
The savings are probably miniscule. But `array $arg=NULL` does allow the parameter to be `NULL`, whereas `array $arg=array()` triggers an error if the parameter is `NULL`.
Peter Ajtai
A: 

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

pleasedontbelong
A: 

=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.

Peter Ajtai
A: 

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.

Helgi Hrafn Gunnarsson