Kill object during construction?

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!

Moderator: General Moderators

Post Reply
rekcor
Forum Newbie
Posts: 2
Joined: Mon Jul 31, 2006 3:48 pm

Kill object during construction?

Post 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...
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

EDIT: nvm.. just seen you are using php4.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

It's called a Factory Method.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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();
}
?>
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Doesn't work.

In PHP 5, you could use exceptions.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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?
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
rekcor
Forum Newbie
Posts: 2
Joined: Mon Jul 31, 2006 3:48 pm

Post by rekcor »

Post Reply