尝试访问非对象的属性(未分配)时没有错误或警告
有人可以向我解释为什么 PHP 在访问空对象的属性(var 未分配)时不报告警告或错误吗?
例如:
$oMyObject->test = 'hello world'; // $oMyObject is not assigned but no warning or error
当我这样做时,它会产生错误:
$oMyObject->test(); // Error: Calling function on non-object
版本信息:
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 Derick Rethans
为什么?尝试设置 error_reporting( E_ALL ) 但仍然没有错误或警告。
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 Derick Rethans
Why? Tried to set error_reporting( E_ALL ) but still no error or warning.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
$oMyObject->text = 'hello world';是一个完全有效的声明,前提是文本被声明为公开的,而不是私有的或受保护的。至于$oMyObject->text(),您需要提供更多信息。你得到什么样的错误?函数 text() 是公共的、私有的还是受保护的?您能发布该功能的作用吗?
$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?
PHP 中的这种行为非常容易出错!
谁真正认为这是一个好主意?
不会立即检测到拼写错误(
$r
而不是$rs
),因为它不是错误。耶皮!
在我看来,这对非/空对象成员访问是完全FUBAR。
在这种情况下,每种理智的语言(PHP 不是)都会给你一个大错误(例如异常、空指针异常、总线错误、分段错误等)。
This behavior in PHP is so error prone!
Who actually thought this would be a good idea?
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.