[TYPO3-core] optional array arguments

Michael Scharkow michael at underused.org
Wed Apr 26 23:56:15 CEST 2006


Martin Kutschker wrote:
> Franz Holzinger <franz at fholzinger.com> writes on 
> Wed, 26 Apr 2006 14:09:51 +0200 (METDST):
> 
>> However if it is an empty array then $variable = array() should get
>> used here. With this you can avoid an additional if to check if the
>> passed parameter is an array.
> 
> 
> No, because it's only guaranteed that the default is an empty array. We have to check because the calling code might pass anything to the function.
> 
> We'd need PHP 5.1 and it's array type hinting feature to avoid the check.

I don't think this is always necessary because most methods can handle 
empty arguments very well. The most frequent problem with passing arrays 
to functions in PHP is the fact that foreach($foo as $bar) does always 
break when $foo is not an array, instead of simply not looping at all, 
as does every better programming language.

IMO type casting/hinting is the wrong solution to this problem because 
loose typing is actually a good thing if implemented correctly. I've 
seen the foreach issue for ages in the PHP bugtracker and really wonder 
why they can't fix it.

Sorry for slight OT,
Michael



More information about the TYPO3-team-core mailing list