Re: foreach with null
| From: | Jochem Maas | Date: | Wed, 26 Oct 2005 22:38:52 +0000 |
| Subject: | Re: foreach with null | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-19750@lists.php.net to get a copy of this message | ||
Ilia Alshanetsky wrote:
IMHO this is a bad idea, if you really wanted to support NULL in a particular case you, as a developer could do: php -r '$n=NULL; foreach((array)$n as $v);' Making NULL usage transparent where array is expects will lead to difficult to debug code.hi :-) the warning for foreach() in this case seems to be inconsistent with: php -r '$a = null; $b = array(); var_dump((empty($a) === empty($b)), (count($a) === count($b)));' which gives no warnings or otherwise, also as a lesser developer than you I don't buy the notion that dropping the warning makes debugging more difficult because advanced code should be checking vars properly before using them and, well, for any given foreach loop there is often enough an 'if (!count($array))'-like statement to take care of the 'empty' case. and in cases when speed is very important it saves an explicit cast (is that more expensive anyway?) and it means not requiring an '@' to suppress which I do know is more expensive than not having any E_* to suppress at all. on the face of it I understand begin against, feedback is good. but removing the warning would make the engine behaviour just a little more consistent - which is nice, no? <sidenote> did anyone remember that php5beta used to allow: function(MyCLass $x = null) {} which was nice from the start fom a user perspective, I was puzzled as to why it went, glad it came back and hope it says :-) </sidenote> thanks for php, and kind regards in general, jochem
-1 for removing the warning. Ilia