Return给我永远真实
Return give me always true
伙计们,我有这个代码:
class Test {
public function __construct($valore) {
if ($valore != TRUE ) {
return false;
} else {
return true;
}
}
}
在另一页中:
$test = new Test("");
if ($test) {
echo "result is: TRUE";
} else {
echo "result is: FALSE";
}
为什么所有时间都是真的??
抱歉,谢谢!
构造函数没有 return 值。所以如果你想测试那个值,你需要有一个方法为你做这个。
class Test
{
private $valore;
public function __construct($valore) {
$this->valore = $valore;
}
public function test() {
return (bool) $valore;
}
}
$test = new Test("");
if ($test->test()) {
echo "result is: TRUE";
} else {
echo "result is: FALSE";
}
它始终为真,因为 $test 对象始终不是假的,它是一个对象。构造函数的 return 值不是您正在测试的值。
class Test {
var $valore;
public function __construct($valore) {
if ($valore != TRUE ) {
$this->valore = false;
} else {
$this->valore = true;
}
}
}
$test = new Test(FALSE);
if ($test->valore === TRUE) {
echo "result is: TRUE";
} else {
echo "result is: FALSE";
}
伙计们,我有这个代码:
class Test {
public function __construct($valore) {
if ($valore != TRUE ) {
return false;
} else {
return true;
}
}
}
在另一页中:
$test = new Test("");
if ($test) {
echo "result is: TRUE";
} else {
echo "result is: FALSE";
}
为什么所有时间都是真的?? 抱歉,谢谢!
构造函数没有 return 值。所以如果你想测试那个值,你需要有一个方法为你做这个。
class Test
{
private $valore;
public function __construct($valore) {
$this->valore = $valore;
}
public function test() {
return (bool) $valore;
}
}
$test = new Test("");
if ($test->test()) {
echo "result is: TRUE";
} else {
echo "result is: FALSE";
}
它始终为真,因为 $test 对象始终不是假的,它是一个对象。构造函数的 return 值不是您正在测试的值。
class Test {
var $valore;
public function __construct($valore) {
if ($valore != TRUE ) {
$this->valore = false;
} else {
$this->valore = true;
}
}
}
$test = new Test(FALSE);
if ($test->valore === TRUE) {
echo "result is: TRUE";
} else {
echo "result is: FALSE";
}