Guys i have this code:
class Test {
public function __construct($valore) {
if ($valore != TRUE ) {
return false;
} else {
return true;
}
}
}
and in another page this:
$test = new Test("");
if ($test) {
echo "result is: TRUE";
} else {
echo "result is: FALSE";
}
Why is all the time true?? Sorry and thank you!
It's always true because the $test object is always NOT false, it's an object. The return value of the constructor isn't what you're testing.
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";
}
Constructors don't have return values. So if you want a to test that value you need to have a method do this for you.
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";
}