php function foo(array $arg = NULL) -- why the array and NULL?

Darryl Hein picture Darryl Hein · Aug 20, 2010 · Viewed 8.7k times · Source

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.

Answer

Colin Hebert picture Colin Hebert · Aug 20, 2010

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.