PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
function createA($b)
{
if (oleIsGod) {
return new A($b);
} else {
return null;
}
}
You may be able to make a function like createA part of the class of A itself but there my knowledge of objects/classes in PHP 4 fails me. And here's where I say get PHP 5 xD
<?php
class A
{
function A($val)
{
if ($val > 5)
{
return true;
}
else
{
return false;
}
}
}
if ($myobj = new A(3))
{
//handle what you are supposed to handle if good
}
else
{
crapout();
}
?>
Wouldn't it seem easier to use the contructor to return true or false, then use the return value code-side to either use the object or not?
Well constructors return the object don't they. So you can't really do that. Am I right, I'm not sure?
I use several classes right now (PHP4 classes) that use that type of logic. They check for something passed to it, set a property and return true, else they return false. I check the setting of the object code-side, and if the construction returns true, I continue, else I die out. Seems to work for me. Not exactly what the OP was asking, but it works for me.
Just a note, but most object models don't allow or support *real* returning of values in a ctor()
Also, I don't believe it's the Ctor() which returns the pointer or reference or object...it's actually new...nothing critical there, but might be helpful...
Self destructing objects...likely not possible inside the ctor() as the object is not technically finished being created/initializing until the ctor() completes execution...
How can you destroy something which doesn't exist?