Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-24-2006, 09:04 PM   PM User | #1
Crimsonjade
New Coder

 
Join Date: Dec 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Crimsonjade is an unknown quantity at this point
Is it ok for my constructors to destroy the object they are making?

I still code with PHP 4 standards for compatibility. I have something like:
PHP Code:
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?

Last edited by Crimsonjade; 12-24-2006 at 09:10 PM..
Crimsonjade is offline   Reply With Quote
Old 12-24-2006, 09:45 PM   PM User | #2
ess
Regular Coder

 
Join Date: Oct 2006
Location: United Kingdom
Posts: 865
Thanks: 7
Thanked 29 Times in 28 Posts
ess will become famous soon enough
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:

PHP Code:
function __destruct() {
// clean references here

ess is offline   Reply With Quote
Old 12-24-2006, 10:26 PM   PM User | #3
Crimsonjade
New Coder

 
Join Date: Dec 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Crimsonjade is an unknown quantity at this point
It is not destructing the object. The object was never constructed to begin with. I am not doing anything different than:
PHP Code:
$my_class = New myClass// no constructor
if ($my_class->init($some_var) != OK) {
    unset(
$my_class); 

Crimsonjade is offline   Reply With Quote
Old 12-25-2006, 08:46 AM   PM User | #4
dumpfi
Regular Coder

 
Join Date: Jun 2004
Posts: 565
Thanks: 0
Thanked 18 Times in 18 Posts
dumpfi will become famous soon enough
Perhaps you should simply use trigger_error() to indicate that there were invalid parameters given to the constructor:
PHP Code:
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
__________________
"Failure is not an option. It comes bundled with the software."
....../)/)..(\__/).(\(\................../)_/)......
.....(-.-).(='.'=).(-.-)................(o.O)...../<)
....(.).(.)("}_("}(.)(.)...............(.)_(.))¯/.
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.
dumpfi is offline   Reply With Quote
Old 12-25-2006, 11:06 PM   PM User | #5
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
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.
__________________
I'm not sure if this was any help, but I hope it didn't make you stupider.

Experience is something you get just after you really need it.
PHP Installation Guide Feedback welcome.
marek_mar is offline   Reply With Quote
Old 12-26-2006, 01:59 AM   PM User | #6
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,911
Thanks: 5
Thanked 80 Times in 79 Posts
firepages will become famous soon enough
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 ?
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is offline   Reply With Quote
Old 12-26-2006, 08:35 PM   PM User | #7
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
Assigning to $this is fatal as of PHP 4.1 (or 4.3)...
__________________
I'm not sure if this was any help, but I hope it didn't make you stupider.

Experience is something you get just after you really need it.
PHP Installation Guide Feedback welcome.
marek_mar is offline   Reply With Quote
Old 12-27-2006, 02:55 AM   PM User | #8
Crimsonjade
New Coder

 
Join Date: Dec 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Crimsonjade is an unknown quantity at this point
Quote:
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.
Quote:
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.

Last edited by Crimsonjade; 12-27-2006 at 02:58 AM..
Crimsonjade is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:37 PM.


Advertisement
Log in to turn off these ads.