Page 1 of 1

I can't explain this [solved]

Posted: Thu Nov 19, 2009 1:34 am
by josh
I have some code that runs as part of an automated test. When I run that code in browser I get E_NOTICE errors, I want to reproduce that behavior in test.

Code: Select all

 
var_dump( $this->value );
        var_dump( $this->value['types'] );
        var_dump( isset( $this->value['types'] ) );
        var_dump( count( $this->value['types'] ) );
 
When I run this code in the test I get:
NULL NULL false int(0)
But no errors (I get errors on "line 2" if I run in the browser.)

If I put count( $foo) instead of the above code it does trigger an E_NOTICE error.

Hmm ok I figured it out but still posting anyways. Turns out

Code: Select all

 
$foo = null;
count( $foo['bar'] )
 
is Valid!

.. Where as..

Code: Select all

 
$foo = array();
count( $foo['bar'] )
 
Will blow up your application in your face

Re: I can't explain this [solved]

Posted: Thu Nov 19, 2009 3:22 am
by jackpf
But...$foo['bar'] doesn't exist, nor is it an array.


EDIT
Oh wait...just saw your example with null. Yeah that is odd :P

Re: I can't explain this [solved]

Posted: Thu Nov 19, 2009 12:26 pm
by josh
Yeah I opened it as a PHP bug and they called bogus.

First they said by accessing it I was somehow creating it?? but that is obviously wrong because I can see $foo is NULL the whole time in that first example

Turns out the "feature" I discovered was
Note: Accessing variables of other types using [] or {} silently returns NULL.
I would just love to see a programmer for a non open-source project try to pass something like that off as not a bug. Silent failures on strings I could understand, but in what possible situation would I have intended to count() on an array index of NULL

Re: I can't explain this [solved]

Posted: Thu Nov 19, 2009 2:11 pm
by jackpf
Yeah, that doesn't make sense. Trying to access an array value of a type that doesn't support it should throw a fatal error imo.

Oh well...