...

View Full Version : Is it ok for my constructors to destroy the object they are making?



Crimsonjade
12-24-2006, 09:04 PM
I still code with PHP 4 standards for compatibility. I have something like:


class myClass {

// constructor
function myClass($some_var)
{
// sanity check $some_var
if ($some_var is NOT sane) {
$this = null; // destroy the class
return;
}

// do something with $some_var
...
}
}


I guess the alternative would be no actions in the constructor and then using some function called init() or something. Using an init() seemed like a useless step to me. The way I am doing it I can easily check the variable being assigned the object when I make the class using is_object().

Are there any potential pitfalls of destroying the object inside the constructor?

ess
12-24-2006, 09:45 PM
This does not make any sense. The constructor is there to instantiate an instance of a given class. It is not ought to act as a destructor by no means.

I am not sure how you can create a destructor in PHP4, but in PHP5, you can create a destructor by using the following syntax:


function __destruct() {
// clean references here
}

Crimsonjade
12-24-2006, 10:26 PM
It is not destructing the object. The object was never constructed to begin with. I am not doing anything different than:


$my_class = New myClass; // no constructor
if ($my_class->init($some_var) != OK) {
unset($my_class);
}

dumpfi
12-25-2006, 08:46 AM
Perhaps you should simply use trigger_error() (http://de2.php.net/manual/en/function.trigger-error.php) to indicate that there were invalid parameters given to the constructor:

class myClass {

// constructor
function myClass($some_var)
{
// sanity check $some_var
if ($some_var is NOT sane) {
trigger_error('myClass::myClass : invalid value for `$somevar`, expected: xxx, given: '.((string) $some_var), E_USER_ERROR);
return;
}

// do something with $some_var
...
}
} dumpfi

marek_mar
12-25-2006, 11:06 PM
The correct way to do this would be to throw an exception in the constructor. This is not possible in PHP4.
You can't assign to $this. AFAIK you have to unset $this and return false (or was it NULL?) for the constructor to fail.

firepages
12-26-2006, 01:59 AM
as you are already doing ...
$this=null;return;
is all you need do, but as mentioned I would assume you would require some error reporting & I would have thought that non-construction would normally be fatal ?

marek_mar
12-26-2006, 08:35 PM
Assigning to $this is fatal as of PHP 4.1 (or 4.3)...

Crimsonjade
12-27-2006, 02:55 AM
as you are already doing ...
$this=null;return;
is all you need do, but as mentioned I would assume you would require some error reporting & I would have thought that non-construction would normally be fatal ?
My plan was the check to see if the variable was indeed an object using is_object. I did not see anything warning me not assign to $this on php.net, but maybe I missed it.


Assigning to $this is fatal as of PHP 4.1 (or 4.3)...
I run php4.4.4 and it works. I see that it does not work in php 5 however. So that is a no go.

Triggering an error really isn't really a route I want to go. I wanted the code to handle the problem, not the user. I guess the init() route it is. No biggie. Thanks for replies.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum