【发布时间】:2013-08-22 13:55:53
【问题描述】:
当你将一个对象传递给 if 时到底发生了什么,像这样:
class TestClass {
...
}
$obj = new TestClass();
if($obj) { // what exactly is going on here ?
...
}
【问题讨论】:
标签: php object type-conversion
当你将一个对象传递给 if 时到底发生了什么,像这样:
class TestClass {
...
}
$obj = new TestClass();
if($obj) { // what exactly is going on here ?
...
}
【问题讨论】:
标签: php object type-conversion
由于您的变量是一个对象,因此将始终被视为 true- 即条件将始终通过,除非 TestClass 的构造函数由于某种原因不会实例化对象。您可以查看type-judging page 以了解 PHP 如何处理类型转换(在本例中,转换为布尔类型)
【讨论】:
true。 IE。除非TestClass 的构造函数由于某种原因不会实例化对象,否则条件将始终通过。
它会告诉你,如果你的$obj 存在
【讨论】:
php 会将您的表达式转换为布尔值。因为这是一个现有对象,所以这将被转换为 TRUE(从 Php 5+ 开始,在 php4 中这将被转换为 FALSE - 我认为):
http://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting
【讨论】: