Object suicude

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Berislav Lopac

    Object suicude

    Is there a way for an object to unset itself in PHP4?

    Of course I could unset it from outside, but I would like to keep the code
    clean. The object has a method which deletes it's "footprint" in the
    database, and as the idea is that we don't need the object once itsin't in
    the DB it would be nice if the same method could get rid of the PHP object
    iself as well.

    Berislav

    --
    If the Internet is a Marx Brothers movie, and Web, e-mail, and IRC are
    Groucho, Chico, and Harpo, then Usenet is Zeppo.


  • Andy Hassall

    #2
    Re: Object suicude

    On Thu, 5 Feb 2004 17:25:28 +0100, "Berislav Lopac" <berislav.lopac @dimedia.hr>
    wrote:
    [color=blue]
    >Is there a way for an object to unset itself in PHP4?
    >
    >Of course I could unset it from outside, but I would like to keep the code
    >clean. The object has a method which deletes it's "footprint" in the
    >database, and as the idea is that we don't need the object once itsin't in
    >the DB it would be nice if the same method could get rid of the PHP object
    >iself as well.[/color]

    The obvious idea of using unset($this) doesn't appear to work:

    <?php
    class Test
    {
    var $x = 'x';
    function pressBigRedButt on() {
    unset($this);
    }
    }

    $test = new Test;
    $test->pressBigRedBut ton();

    echo $test->x;
    ?>

    (outputs 'x', rather than a warning).

    --
    Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
    <http://www.andyh.co.uk > / <http://www.andyhsoftwa re.co.uk/space>

    Comment

    • Chung Leong

      #3
      Re: Object suicude

      unset($this) only remove the variable "this" from the current namespace.
      What you want to do is $this = null. That sets what $this was pointing to to
      null.

      Uzytkownik "Andy Hassall" <andy@andyh.co. uk> napisal w wiadomosci
      news:c5i5205qo4 knujmnral6bu4lq 252ncgecd@4ax.c om...[color=blue]
      > On Thu, 5 Feb 2004 17:25:28 +0100, "Berislav Lopac"[/color]
      <berislav.lopac @dimedia.hr>[color=blue]
      > wrote:
      >[color=green]
      > >Is there a way for an object to unset itself in PHP4?
      > >
      > >Of course I could unset it from outside, but I would like to keep the[/color][/color]
      code[color=blue][color=green]
      > >clean. The object has a method which deletes it's "footprint" in the
      > >database, and as the idea is that we don't need the object once itsin't[/color][/color]
      in[color=blue][color=green]
      > >the DB it would be nice if the same method could get rid of the PHP[/color][/color]
      object[color=blue][color=green]
      > >iself as well.[/color]
      >
      > The obvious idea of using unset($this) doesn't appear to work:
      >
      > <?php
      > class Test
      > {
      > var $x = 'x';
      > function pressBigRedButt on() {
      > unset($this);
      > }
      > }
      >
      > $test = new Test;
      > $test->pressBigRedBut ton();
      >
      > echo $test->x;
      > ?>
      >
      > (outputs 'x', rather than a warning).
      >
      > --
      > Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
      > <http://www.andyh.co.uk > / <http://www.andyhsoftwa re.co.uk/space>[/color]


      Comment

      • Phil Powell

        #4
        Re: Object suicude

        Have a question about $this = null:

        In PHP is this its equivalent of destroying an object, say, if you
        will, Garbage Collection? I am working on a project that has anywhere
        between 10 - 50 different instances occurring at one time (I kid you
        not!) and realize this will turn into a horrific bottleneck unless
        there is some means of garbage collection.

        Thanx
        Phil

        "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message news:<vMGdnQV_P eytTb_dRVn-uA@comcast.com> ...[color=blue]
        > unset($this) only remove the variable "this" from the current namespace.
        > What you want to do is $this = null. That sets what $this was pointing to to
        > null.
        >
        > Uzytkownik "Andy Hassall" <andy@andyh.co. uk> napisal w wiadomosci
        > news:c5i5205qo4 knujmnral6bu4lq 252ncgecd@4ax.c om...[color=green]
        > > On Thu, 5 Feb 2004 17:25:28 +0100, "Berislav Lopac"[/color]
        > <berislav.lopac @dimedia.hr>[color=green]
        > > wrote:
        > >[color=darkred]
        > > >Is there a way for an object to unset itself in PHP4?
        > > >
        > > >Of course I could unset it from outside, but I would like to keep the[/color][/color]
        > code[color=green][color=darkred]
        > > >clean. The object has a method which deletes it's "footprint" in the
        > > >database, and as the idea is that we don't need the object once itsin't[/color][/color]
        > in[color=green][color=darkred]
        > > >the DB it would be nice if the same method could get rid of the PHP[/color][/color]
        > object[color=green][color=darkred]
        > > >iself as well.[/color]
        > >
        > > The obvious idea of using unset($this) doesn't appear to work:
        > >
        > > <?php
        > > class Test
        > > {
        > > var $x = 'x';
        > > function pressBigRedButt on() {
        > > unset($this);
        > > }
        > > }
        > >
        > > $test = new Test;
        > > $test->pressBigRedBut ton();
        > >
        > > echo $test->x;
        > > ?>
        > >
        > > (outputs 'x', rather than a warning).
        > >
        > > --
        > > Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
        > > <http://www.andyh.co.uk > / <http://www.andyhsoftwa re.co.uk/space>[/color][/color]

        Comment

        • Andy Hassall

          #5
          Re: Object suicude

          On Thu, 5 Feb 2004 18:16:47 -0500, "Chung Leong" <chernyshevsky@ hotmail.com>
          wrote:
          [color=blue]
          >unset($this) only remove the variable "this" from the current namespace.
          >What you want to do is $this = null. That sets what $this was pointing to to
          >null.[/color]

          Ah - thanks, I wasn't aware of the distinction. Looks like this is what Phil
          is looking for, as a quick test appears to show it does destroy the object.

          --
          Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
          <http://www.andyh.co.uk > / <http://www.andyhsoftwa re.co.uk/space>

          Comment

          • Chung Leong

            #6
            Re: Object suicude

            In theory, yes. In practice, I have not a clue. On my server the Apache
            process regularly chew up half a gig of memory.

            Uzytkownik "Phil Powell" <soazine@erols. com> napisal w wiadomosci
            news:1cdca2a7.0 402061306.26974 f67@posting.goo gle.com...[color=blue]
            > Have a question about $this = null:
            >
            > In PHP is this its equivalent of destroying an object, say, if you
            > will, Garbage Collection? I am working on a project that has anywhere
            > between 10 - 50 different instances occurring at one time (I kid you
            > not!) and realize this will turn into a horrific bottleneck unless
            > there is some means of garbage collection.
            >
            > Thanx
            > Phil
            >
            > "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message[/color]
            news:<vMGdnQV_P eytTb_dRVn-uA@comcast.com> ...[color=blue][color=green]
            > > unset($this) only remove the variable "this" from the current namespace.
            > > What you want to do is $this = null. That sets what $this was pointing[/color][/color]
            to to[color=blue][color=green]
            > > null.
            > >
            > > Uzytkownik "Andy Hassall" <andy@andyh.co. uk> napisal w wiadomosci
            > > news:c5i5205qo4 knujmnral6bu4lq 252ncgecd@4ax.c om...[color=darkred]
            > > > On Thu, 5 Feb 2004 17:25:28 +0100, "Berislav Lopac"[/color]
            > > <berislav.lopac @dimedia.hr>[color=darkred]
            > > > wrote:
            > > >
            > > > >Is there a way for an object to unset itself in PHP4?
            > > > >
            > > > >Of course I could unset it from outside, but I would like to keep the[/color]
            > > code[color=darkred]
            > > > >clean. The object has a method which deletes it's "footprint" in the
            > > > >database, and as the idea is that we don't need the object once[/color][/color][/color]
            itsin't[color=blue][color=green]
            > > in[color=darkred]
            > > > >the DB it would be nice if the same method could get rid of the PHP[/color]
            > > object[color=darkred]
            > > > >iself as well.
            > > >
            > > > The obvious idea of using unset($this) doesn't appear to work:
            > > >
            > > > <?php
            > > > class Test
            > > > {
            > > > var $x = 'x';
            > > > function pressBigRedButt on() {
            > > > unset($this);
            > > > }
            > > > }
            > > >
            > > > $test = new Test;
            > > > $test->pressBigRedBut ton();
            > > >
            > > > echo $test->x;
            > > > ?>
            > > >
            > > > (outputs 'x', rather than a warning).
            > > >
            > > > --
            > > > Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
            > > > <http://www.andyh.co.uk > / <http://www.andyhsoftwa re.co.uk/space>[/color][/color][/color]


            Comment

            • Alex Farran

              #7
              Re: Object suicude

              Andy Hassall writes:
              [color=blue]
              > On Thu, 5 Feb 2004 18:16:47 -0500, "Chung Leong" <chernyshevsky@ hotmail.com>
              > wrote:[/color]
              [color=blue][color=green]
              >> unset($this) only remove the variable "this" from the current namespace.
              >> What you want to do is $this = null. That sets what $this was pointing to to
              >> null.[/color][/color]
              [color=blue]
              > Ah - thanks, I wasn't aware of the distinction. Looks like this is what Phil
              > is looking for, as a quick test appears to show it does destroy the object.[/color]

              Well that is interesting. I had to try it myself. Conceptually $this
              is just another parameter passed by reference, and you can do anything
              you want to it, including assigning it a completely different object.

              I adapted the suicide test to do this -

              <?php
              class Transmute {
              var $y = 'y';
              }

              class Test {
              var $x = 'x';
              function pressBigRedButt on() {
              $this= new Transmute();
              }
              }
              $test = new Test;
              echo $test->x;
              $test->pressBigRedBut ton();
              echo $test->x;
              echo $test->y;
              ?>

              Returns

              x
              Notice: Undefined property: x in suicide.php on line 16
              y

              --

              __o Alex Farran
              _`\<,_ Analyst / Programmer
              (_)/ (_) www.alexfarran.com

              Comment

              • Zurab Davitiani

                #8
                Re: Object suicude

                Phil Powell wrote:
                [color=blue]
                > Have a question about $this = null:
                >
                > In PHP is this its equivalent of destroying an object, say, if you
                > will, Garbage Collection? I am working on a project that has anywhere
                > between 10 - 50 different instances occurring at one time (I kid you
                > not!) and realize this will turn into a horrific bottleneck unless
                > there is some means of garbage collection.[/color]

                There's none in PHP4. Even if you do $someObject = null or unset() or
                anything else it will not free up memory used by that object. Memory is
                only freed up when the script execution ends.

                This only applies to objects and not other types like arrays, strings, etc.

                Comment

                • Alex Farran

                  #9
                  Re: Object suicude

                  Zurab Davitiani writes:
                  [color=blue]
                  > Phil Powell wrote:[color=green]
                  >> Have a question about $this = null:
                  >>
                  >> In PHP is this its equivalent of destroying an object, say, if you
                  >> will, Garbage Collection? I am working on a project that has anywhere
                  >> between 10 - 50 different instances occurring at one time (I kid you
                  >> not!) and realize this will turn into a horrific bottleneck unless
                  >> there is some means of garbage collection.[/color][/color]
                  [color=blue]
                  > There's none in PHP4. Even if you do $someObject = null or unset() or
                  > anything else it will not free up memory used by that object. Memory is
                  > only freed up when the script execution ends.[/color]

                  What makes you think that? PHP4's garbage collection algorithm is an
                  unsophisticated reference counter, so it may not be the most efficient
                  in the world, but I'd be very surprised if objects weren't GC'd.
                  --

                  __o Alex Farran
                  _`\<,_ Analyst / Programmer
                  (_)/ (_) www.alexfarran.com

                  Comment

                  • Zurab Davitiani

                    #10
                    Re: Object suicude

                    Alex Farran wrote:
                    [color=blue][color=green]
                    >> There's none in PHP4. Even if you do $someObject = null or unset() or
                    >> anything else it will not free up memory used by that object. Memory is
                    >> only freed up when the script execution ends.[/color]
                    >
                    > What makes you think that? PHP4's garbage collection algorithm is an
                    > unsophisticated reference counter, so it may not be the most efficient
                    > in the world, but I'd be very surprised if objects weren't GC'd.[/color]

                    I can't see how they are. I've tried few scripts quite a few times and
                    different ways that create new objects in a for-loop. Memory consumption
                    never goes down, but only increases as the script continues to run.
                    Eventually, the available memory is used up and you either get a fatal
                    error (unable to allocate memory), or the process simply crashes, depending
                    on your version of PHP4.

                    This only happens with objects, but not with other types, as I said; even if
                    you try to unset or =null or reassign the same variable name to another
                    object - doesn't matter how you do it.

                    I'd be interested to see what method you are suggesting to use that will
                    properly destroy the object, freeing the associated memory. It just doesn't
                    happen automatically with the garbage collection like with the other types
                    of variables.

                    Comment

                    • Alex Farran

                      #11
                      Re: Object suicude

                      Zurab Davitiani writes:
                      [color=blue]
                      > I'd be interested to see what method you are suggesting to use that will
                      > properly destroy the object, freeing the associated memory. It just doesn't
                      > happen automatically with the garbage collection like with the other types
                      > of variables.[/color]

                      Well I was arguing from my understanding of the garbage collection
                      mechanism rather than practical experience, but I ran simple test that
                      proves that objects are garbage collected just like any other block of
                      memory. It wouldn't be a very practical object system if they
                      weren't.

                      The PHP4 garbage collector uses a reference counting algorithm. What
                      this means is that any block of memory, be it an array, object, string
                      or integer, is freed when there are no references to it. This works
                      well for simple cases, but can be fooled by cyclic references. If two
                      objects reference each other, or one object references itself, then
                      the reference count will always be more than 0 so the object(s) will
                      never be GC'd.

                      Here are my tests -

                      This script will run to completion, since each time a new object is
                      created the reference to the previous object is destroyed.

                      echo "start\n";
                      for ($i = 1; $i<100000; $i++ ) {
                      $mem = new memory();
                      }
                      echo "end\n";

                      This script will run out of memory because the reference to each
                      object is retained.

                      echo "start\n";
                      for ($i = 1; $i<100000; $i++ ) {
                      $mem[$i] = new memory();
                      }
                      echo "end\n";

                      This will also fail because each object contains a reference to
                      itself.

                      echo "start\n";
                      for ($i = 1; $i<100000; $i++ ) {
                      $mem =& new memory();
                      $mem->a =& $mem;
                      }
                      echo "end\n";

                      --

                      __o Alex Farran
                      _`\<,_ Analyst / Programmer
                      (_)/ (_) www.alexfarran.com

                      Comment

                      • Jochen Daum

                        #12
                        Re: Object suicude

                        Hi Berislav!

                        On Thu, 5 Feb 2004 17:25:28 +0100, "Berislav Lopac"
                        <berislav.lopac @dimedia.hr> wrote:
                        [color=blue]
                        >Is there a way for an object to unset itself in PHP4?
                        >
                        >Of course I could unset it from outside, but I would like to keep the code
                        >clean. The object has a method which deletes it's "footprint" in the
                        >database, and as the idea is that we don't need the object once itsin't in
                        >the DB it would be nice if the same method could get rid of the PHP object
                        >iself as well.
                        >
                        >Berislav[/color]

                        I have been loking at PostNuke recently and they include everything in
                        a function. Though they don't use objects, everything is gone and out
                        of scope, once the function ends.

                        HTH, Jochen

                        --
                        Jochen Daum - Cabletalk Group Ltd.
                        PHP DB Edit Toolkit -- PHP scripts for building
                        database editing interfaces.
                        Download PHP DB Edit Toolkit for free. PHP DB Edit Toolkit is a set of PHP classes makes the generation of database edit interfaces easier and faster. The main class builds tabular and form views based on a data dictionary and takes over handling of insert/update/delete and user input.

                        Comment

                        • Zurab Davitiani

                          #13
                          Re: Object suicude

                          Alex Farran wrote:
                          [color=blue]
                          > Well I was arguing from my understanding of the garbage collection
                          > mechanism rather than practical experience, but I ran simple test that
                          > proves that objects are garbage collected just like any other block of
                          > memory. It wouldn't be a very practical object system if they
                          > weren't.
                          >
                          > The PHP4 garbage collector uses a reference counting algorithm. What
                          > this means is that any block of memory, be it an array, object, string
                          > or integer, is freed when there are no references to it. This works
                          > well for simple cases, but can be fooled by cyclic references. If two
                          > objects reference each other, or one object references itself, then
                          > the reference count will always be more than 0 so the object(s) will
                          > never be GC'd.
                          >
                          > Here are my tests -
                          >
                          > This script will run to completion, since each time a new object is
                          > created the reference to the previous object is destroyed.
                          >
                          > echo "start\n";
                          > for ($i = 1; $i<100000; $i++ ) {
                          > $mem = new memory();
                          > }
                          > echo "end\n";
                          >
                          > This script will run out of memory because the reference to each
                          > object is retained.
                          >
                          > echo "start\n";
                          > for ($i = 1; $i<100000; $i++ ) {
                          > $mem[$i] = new memory();
                          > }
                          > echo "end\n";
                          >
                          > This will also fail because each object contains a reference to
                          > itself.
                          >
                          > echo "start\n";
                          > for ($i = 1; $i<100000; $i++ ) {
                          > $mem =& new memory();
                          > $mem->a =& $mem;
                          > }
                          > echo "end\n";
                          >[/color]

                          This doesn't seem to work on all objects; and I am confused that it works on
                          some but not others. I was trying to determine exactly what objects have to
                          contain for PHP GC not to be able to release the memory, but I was unable
                          to create a simple test case scenario in a limited free time I had. I will
                          research it more, and hopefully have more details, and probably start
                          another thread when I do. Thanks for the info!

                          Comment

                          Working...