The return value of the constructor is ignored by the new
operator. The new
operator will always return an object. When having code like
class A {
function __construct() {
return 1;
}
}
class B extends A {
function __construct() {
var_dump(parent::__construct());
}
}
new B();
you can see that the constructor has a return value. Writing such code makes, of course, little sense.
For handling errors inside a constructor you'd have to use exceptions. An alternative approach is some kind of factory method.
class C {
private function __construct() { }
private function init() {
if (something_goes_wrong()) {
return false;
}
}
public static createInstance() {
$retval = new self;
if (!$retval->init()) {
return false;
}
return $retval;
}
}