PHP: Destroy objects? Destructors?

Discussion in 'Web Design and Development' started by ppc_michael, Jul 5, 2007.

  1. ppc_michael Guest

    ppc_michael

    Joined:
    Apr 26, 2005
    Location:
    Los Angeles, CA
    #1
    Is there a way to destroy objects (besides letting them go out of scope, I guess)? If so, is there a way to define a destructor?
     
  2. ppc_michael thread starter Guest

    ppc_michael

    Joined:
    Apr 26, 2005
    Location:
    Los Angeles, CA
    #3
  3. CoreWeb macrumors 6502

    Joined:
    Mar 2, 2007
    Location:
    Edge of reason
    #4
    There are no destructors in PHP4, but you can destroy objects using unset. However, I would imagine that one could simulate destructors by calling a custom function. Also PHP.net claims that using register_shutdown_function can simulate destruct for when an object goes out of scope. Note, however, I am no expert in PHP4.

    PHP:
    $class = new myClass();
    $class->destroy();
    unset(
    $class);
     
  4. Winterfell macrumors regular

    Joined:
    Apr 3, 2007
    Location:
    Tulsa, Oklahoma
    #5
    I always just clean up objects that I'm no longer using by setting them to null. This does two things: removes all references to said object, allowing PHP to destroy the object cleanly. It also keeps you from having to call unset(), which does the same thing, except you won't have the overhead of calling another function.

    All in all though, the PHP interpreter is pretty smart, and knows when to remove an object from memory. :)
     
  5. ppc_michael thread starter Guest

    ppc_michael

    Joined:
    Apr 26, 2005
    Location:
    Los Angeles, CA
    #6
    Thanks to both of you for the replies! They were most helpful. The register_shutdown_function does sound like the closest thing I'll get to a destructor.

    Thanks!!
     

Share This Page