Entry
Can I return a value from a class constructor?
How can I return an error from a class constructor?
Jul 11th, 1999 05:42
Nuutti-Iivari Meriläinen, Nathan Wallace,
The class constructor is executed when creating an instance of the
class. It always returns the new class instance.
If you need to return something from the constructor then you can work
around this. For every PHP class you create, have a variable called
$retval. Then, set $retval inside the constructor to whatever you would
want to return. Then, after calling $x = new foo() - check the value of
$x->retval.
One case where you may like to return a different value from a
constructor is if an error occurs while creating the object. There is
one way to do what you want to do (I assume that you want to check
whether the instance was initialized correctly):
--- BEGIN PSEUDOCODE ---
class <classname>
{
function <classname>(<init args>,&$error)
{
if(<init goes ok>) $error=false;
else $error=true;
}
}
$instance= new <classname>(<init args>,$error);
if($error==true) <handle the error>
else <use the object>
--- END PSEUDOCODE ---
Thus, when you call the constructor of the class you pass a reference
to an error checking variable that will contain (in the above case) true
or false, depending on whether the instance was initialized correctly or
not. Of course, you can make more elaborate checking mechanisms, but the
above shows the principle.
Just to illustrate, here is a working example that you can use as
a basis for further development:
--- BEGIN PHP CODE ---
<?PHP
class FortyTwo
{
var $value;
var $isValid=false;
function FortyTwo($number,&$error)
{
if($number==42) {
$this->isValid=true;
$this->value=$number;
$error=false;
} else $error=true;
}
function getValue()
{
if($this->isValid) return($this->value);
else return(0);
}
}
// This test fails
$fortyTwo=new FortyTwo(41,$error);
if($error) printf("There was an error while initializing the
object.<BR>");
else printf("The object was initialized correctly, the value is %d<BR>",
$fortyTwo->getValue());
// This test succeeds
$fortyTwo=new FortyTwo(42,$error);
if($error) printf("There was an error while initializing the
object.<BR>");
else printf("The object was initialized correctly, the value is %d<BR>",
$fortyTwo->getValue());
?>
--- END PHP CODE ---
Of course, the method of using object variables for the error codes
and messages will suffice, but I think the above method is what you are
after (as you do not need to have extra variables inside the object that
you have to check). I think this can be used in subclasses as well to
check whether the parent class was initialized... but I digress.
(Actually, I am using this somewhere the other way around - I am
calling
a function that may or may not return an object. Thus -
--- BEGIN PSEUDOCODE ---
function getSomeObject(<arguments>,&$objRef)
{
if(<object is ok>) return(true);
else {
$objRef=false;
return($objRef);
}
}
if($retcode=getSomeObject(<arguments>,&$objRef)) <use the object>
else <handle the error>
--- END PSEUDOCODE ---
...well, that is a very simple example but probably illustrates what I
am doing with the variable reference.)