destructors

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike G

    destructors

    So if I don't have anything specific to do when an object is no
    longer referenced, I don't have to explicitly define a destructor
    method? I am not required to free or delete each variable, am I?

    Thanks, Mike



    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
  • Joachim Weiß

    #2
    Re: destructors

    Mike G schrieb:[color=blue]
    > So if I don't have anything specific to do when an object is no
    > longer referenced, I don't have to explicitly define a destructor
    > method? I am not required to free or delete each variable, am I?[/color]
    Normally you don't need a destructor. Because objects are only valid in
    their scope.

    function foo() {
    $bar = new Something()
    }

    $bar will be created and destroyed everytime foo is called.

    However you may want to have an explitzit destructor:
    eg.

    class Something{
    var $fp;
    // constructor
    function Something(
    $this->fp=fopen("some file","r");
    }

    // now you need to close the file when something is destroyed

    //destructor
    // watch the underscore
    function _Something(
    fclose($this->fp);
    }


    Hope it helps!

    Jo

    Comment

    • Michael G

      #3
      Re: destructors

      "Mike G" <mike-g@montana.com> wrote in message
      news:1124208308 _2917@spool6-east.superfeed. net...[color=blue]
      > So if I don't have anything specific to do when an object is no
      > longer referenced, I don't have to explicitly define a destructor
      > method? I am not required to free or delete each variable, am I?
      >[/color]

      In terms of freeing memory it makes sense that it isn't really an
      issue with web applications. Once a request (script) has been
      responded to, the server automagically restores the memory
      used by the script.

      Mike



      ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
      http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
      ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

      Comment

      • Janwillem Borleffs

        #4
        Re: destructors

        Joachim Weiß wrote:[color=blue]
        > //destructor
        > // watch the underscore
        > function _Something(
        > fclose($this->fp);
        > }
        >[/color]

        I'm watching it, but all I can see is that you possibly mean the
        __destruct() method available in PHP5...


        JW



        Comment

        • Joachim Weiß

          #5
          Re: destructors

          Janwillem Borleffs schrieb:[color=blue]
          > Joachim Weiß wrote:
          >[color=green]
          >>//destructor
          >>// watch the underscore
          >>function _Something(
          >>fclose($thi s->fp);
          >>}
          >>[/color]
          >
          >
          > I'm watching it, but all I can see is that you possibly mean the
          > __destruct() method available in PHP5...
          >
          >
          > JW
          >
          >
          >[/color]
          Your right! I mixed it up with something else!
          class Something{
          var $fp;

          class Something {
          function __construct() {
          $this->fp=fopen("some file","r");
          }

          function __destruct() {
          fclose($this->fp);
          }

          }

          Comment

          • Peter Fox

            #6
            Re: destructors

            Following on from Joachim Weiß's message. . .[color=blue]
            >function foo() {
            > $bar = new Something()
            >}
            >
            >$bar will be created and destroyed everytime foo is called.[/color]

            Q: As far as the PHP script is concerned $bar no longer exists and if
            foo() is called again another $bar is created - That's fine.

            But what is the garbage collection situation during the running of the
            script, which could be important for large objects?


            --
            PETER FOX Not the same since the bookshop idea was shelved
            peterfox@eminen t.demon.co.uk.n ot.this.bit.no. html
            2 Tees Close, Witham, Essex.
            Gravity beer in Essex <http://www.eminent.dem on.co.uk>

            Comment

            Working...