Matt Rose
Matt Rose

Reputation: 536

PHP return value... what am I missing?

Shouldn't the function return value ($checkZero) be false (boolean)? The result of the following is 'zero is zero'. What am I missing?

class CheckZero {
    function __construct() {
        $zero = 3;
        if ($zero === 0) {
            return true;
        }
        else {
            return false;
        }
    }
}

$checkZero = new CheckZero();
if (!$checkZero) {
    echo 'zero is not zero';
}
else {
    echo 'zero is zero';
}

Upvotes: 0

Views: 118

Answers (2)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324620

Constructor prototype:

void __construct ([ mixed $args [, $... ]] )

This means that the return value of __construct is discarded and useless, because it is the object itself that is returned.

If you run var_dump($checkZero); then you would see that it is an object of clas CheckZero.

Upvotes: 0

user142162
user142162

Reputation:

You can't return from a class constructor. What is returned is the newly created object.

If you var_dump the return value, you'll see that an object was returned:

object(CheckZero)#1 (0) {
}

Upvotes: 1

Related Questions