No error or warning when attempt to access property of non object (not assigned)
Can somebody explain to me why PHP does not report a warning or error when accessing a property of a empty object (var is not assigned)?
For example:
$oMyObject->test = 'hello world'; // $oMyObject is not assigned but no warning or error
When i do this, it produces an error:
$oMyObject->test(); // Error: Calling function on non-object
Version info:
Windows XP
XAMPP Windows Version 1.7.0
Apache/2.2.11 (Win32)
PHP 5.2.8
Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies
with Xdebug v2.0.4, Copyright (c) 2002-2008, by D开发者_如何学Cerick Rethans
Why? Tried to set error_reporting( E_ALL ) but still no error or warning.
$oMyObject->text = 'hello world'; is a completely valid statement, provided text is declared public and not private or protected. As for $oMyObject->text(), you need to provide some more information. What kind of error you get? Is the function text() public or private or protected? Can you post what the function does?
This behavior in PHP is so error prone! Who actually thought this would be a good idea?
$rs = $db->Execute("SELECT 1");
while(!$r->EOF) {
// runs forever
$rs->MoveNext();
}
The typo ($r
instead $rs
) isn't detected immediately, because it's no error.
YIPPIE!
In my opinion, this is non/null-object member access is complete FUBAR. Every sane language (which PHP is not) gives you a big fat error (eg. exception, nullpointerexception, bus error, Segmentation fault, etc) in that case.
精彩评论