Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Dec 2006
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    866
    Thanks
    7
    Thanked 30 Times in 29 Posts
    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


  • #3
    New Coder
    Join Date
    Dec 2006
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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); 


  • #4
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    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.

  • #5
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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.

  • #6
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,081
    Thanks
    11
    Thanked 99 Times in 97 Posts
    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)

  • #7
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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.

  • #8
    New Coder
    Join Date
    Dec 2006
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Last edited by Crimsonjade; 12-27-2006 at 02:58 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •