• 6

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

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.

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.

  • 7
Reply Report
      • 2
    • 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.

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.

  • 4
Reply Report
      • 1
    • No. Both = NULL and = array() allow for no arguments, but only =NULL allows for the argument to be NULL.

=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

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

  • 2
Reply Report

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.

  • 1
Reply Report

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){ ... }
  • 1
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions