Page 1 of 1

Kill object during construction?

Posted: Mon Jul 31, 2006 3:53 pm
by rekcor
Is it possible in PHP4 to kill an object during its construction?

Something like this:

Code: Select all

class A
{
  function A ($b)
  {
     if ($b<5)
     {
        // do not create object, because $b has to be >=5
     }
     else
     {
        // create object
     }
  }
}
Have been looking on Php.net, forums and even Google, but couldn't find anything...

Posted: Mon Jul 31, 2006 4:28 pm
by Ollie Saunders
Not in a constructor its not.
Use an extenal function to create it:

Code: Select all

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

Posted: Mon Jul 31, 2006 4:57 pm
by Jenk
EDIT: nvm.. just seen you are using php4.

Posted: Mon Jul 31, 2006 5:05 pm
by Ambush Commander
It's called a Factory Method.

Posted: Mon Jul 31, 2006 5:14 pm
by RobertGonzalez
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?

Code: Select all

<?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();
}
?>

Posted: Mon Jul 31, 2006 5:15 pm
by Ambush Commander
Doesn't work.

In PHP 5, you could use exceptions.

Posted: Mon Jul 31, 2006 5:32 pm
by Ollie Saunders
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?

Posted: Mon Jul 31, 2006 7:08 pm
by sweatje
A completely non-forward compatible solution is to assign

Code: Select all

$this = null;
in your constructor.

Using this code will make you want to beat yourself with a stick when you try to upgrade to php5.

Posted: Mon Jul 31, 2006 7:28 pm
by RobertGonzalez
ole wrote:
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.

Posted: Mon Jul 31, 2006 8:33 pm
by alex.barylski
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? :P

Posted: Tue Aug 01, 2006 3:05 am
by Chris Corbyn
sweatje wrote:A completely non-forward compatible solution is to assign

Code: Select all

$this = null;
in your constructor.

Using this code will make you want to beat yourself with a stick when you try to upgrade to php5.
That just seems as poor as doing this inside an object :) I know PhpDocumentor broke when PHP5 was released because they did something like this...

Code: Select all

$foo = new someObject();
$this = $foo;
Use a factory method like Ambush says... you can stick it inside your class and just call it statically.

Posted: Tue Aug 01, 2006 8:30 am
by rekcor