OOP: unset($this) or maybe unset(&$this)

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

    OOP: unset($this) or maybe unset(&$this)

    Just wondering,

    Is it possible for an instance of a class to unset itself?
    can I do "unset($thi s)" or "unset(&$th is)"

    regards,
    Lieven
    http://eye.cc php newsgroups
  • Tony Marston

    #2
    Re: unset($this) or maybe unset(&$thi s)

    Don't be silly. The execution path is sequential, so after a command which
    does not involve a jump is executed the next instruction to be executed will
    be the very next instruction in the same object method. If you have just
    erased the object that contains the next instruction what do you think will
    happen? How is the PHP processor supposed to know where to go?

    --
    Tony Marston

    This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL




    "lievendp" <lievendp@pando ra-dot-be.no-spam.invalid> wrote in message
    news:dtdvpg$11i u$2@news3.infoa ve.net...[color=blue]
    > Just wondering,
    >
    > Is it possible for an instance of a class to unset itself?
    > can I do "unset($thi s)" or "unset(&$th is)"
    >
    > regards,
    > Lieven
    > http://eye.cc php newsgroups[/color]


    Comment

    • Chung Leong

      #3
      Re: OOP: unset($this) or maybe unset(&amp;$thi s)

      No, unset($this) will only remove $this from variable space of the
      current function scope. The object will remain in the caller scope.

      You can do a $this = null to overwrite the object however.

      Comment

      • Oli Filth

        #4
        Re: unset($this) or maybe unset(&amp;$thi s)

        Tony Marston said the following on 21/02/2006 09:46:[color=blue]
        > Don't be silly. The execution path is sequential, so after a command which
        > does not involve a jump is executed the next instruction to be executed will
        > be the very next instruction in the same object method. If you have just
        > erased the object that contains the next instruction what do you think will
        > happen? How is the PHP processor supposed to know where to go?
        >[/color]

        I dunno about PHP, but the equivalent is perfectly possible in C++, i.e.
        you can call delete on this (assuming it's a heap-based object).

        The object and its method code are not one and the same thing. Deleting
        an object doesn't mean that the code disappears...

        However, like I said, I don't know what PHP allows you to do or not.


        --
        Oli

        Comment

        • Tony Marston

          #5
          Re: unset($this) or maybe unset(&amp;$thi s)


          "Oli Filth" <catch@olifilth .co.uk> wrote in message
          news:0F7Lf.5940 4$mf2.36568@new sfe6-win.ntli.net...[color=blue]
          > Tony Marston said the following on 21/02/2006 09:46:[color=green]
          >> Don't be silly. The execution path is sequential, so after a command
          >> which does not involve a jump is executed the next instruction to be
          >> executed will be the very next instruction in the same object method. If
          >> you have just erased the object that contains the next instruction what
          >> do you think will happen? How is the PHP processor supposed to know where
          >> to go?
          >>[/color]
          >
          > I dunno about PHP, but the equivalent is perfectly possible in C++, i.e.
          > you can call delete on this (assuming it's a heap-based object).
          >
          > The object and its method code are not one and the same thing. Deleting an
          > object doesn't mean that the code disappears...[/color]

          An object is comprised of methods (code) and properties (variables). If you
          delete/unset an object then both disappear as all their reference points no
          longer exist. The code may still exist in the class definition, but the
          object, which contains a copy of that code in memory, does not, so how can
          it continue executing any of that code?

          Deleting an object while you are still inside it is like blowing up a house
          when you are still inside - not a good idea!


          Comment

          • Oli Filth

            #6
            Re: unset($this) or maybe unset(&amp;$thi s)

            Tony Marston said the following on 23/02/2006 10:02:[color=blue]
            > "Oli Filth" <catch@olifilth .co.uk> wrote in message
            > news:0F7Lf.5940 4$mf2.36568@new sfe6-win.ntli.net...[color=green]
            >> Tony Marston said the following on 21/02/2006 09:46:[color=darkred]
            >>> Don't be silly. The execution path is sequential, so after a command
            >>> which does not involve a jump is executed the next instruction to be
            >>> executed will be the very next instruction in the same object method. If
            >>> you have just erased the object that contains the next instruction what
            >>> do you think will happen? How is the PHP processor supposed to know where
            >>> to go?
            >>>[/color]
            >> I dunno about PHP, but the equivalent is perfectly possible in C++, i.e.
            >> you can call delete on this (assuming it's a heap-based object).
            >>
            >> The object and its method code are not one and the same thing. Deleting an
            >> object doesn't mean that the code disappears...[/color]
            >
            > An object is comprised of methods (code) and properties (variables). If you
            > delete/unset an object then both disappear as all their reference points no
            > longer exist. The code may still exist in the class definition, but the
            > object, which contains a copy of that code in memory, does not, so how can
            > it continue executing any of that code?[/color]

            Well, in C++, objects most definitely don't keep a copy of the method
            code in memory. When you call something like obj.Func(var); in C++
            (assuming obj is an instance of class Foo), the compiler actually
            translates that to something like:

            __Foo__Func(&ob j, var);

            with the method actually just equivalent to a normal global function,
            internally defined as something like:

            void __Foo__Func(str uct Foo *this, int var)
            {
            ...
            }

            and Foo internally defined as:

            struct Foo
            {
            /* member variables of Foo */
            }

            Calling delete this; just deallocates the storage space set aside for
            the Foo struct. Nothing at all happens to the code.

            I'd like to think that something similar occurs in PHP, as making a
            "copy" of the code every time you create an object would be a waste of
            time and memory.

            [color=blue]
            > Deleting an object while you are still inside it is like blowing up a house
            > when you are still inside - not a good idea![/color]

            Going back to C++, use of "delete this" is quite a common practice in
            smart-pointer/reference-counting classes, or objects designed not to
            have any other references to them.


            --
            Oli

            Comment

            • Tony Marston

              #7
              Re: unset($this) or maybe unset(&amp;$thi s)

              PHP is not C++, so expecting the same behaviour from two different languages
              is just being too optimistic. It is not wise to delete an object while you
              are still inside it, nor is it wise to overwrite it with something else
              while you are still inside it. Why? Because it buggers up any references to
              the original object which may still exist in other places.

              --
              Tony Marston
              This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL


              "Oli Filth" <catch@olifilth .co.uk> wrote in message
              news:kAiLf.4267 4$m13.34512@new sfe5-gui.ntli.net...[color=blue]
              > Tony Marston said the following on 23/02/2006 10:02:[color=green]
              >> "Oli Filth" <catch@olifilth .co.uk> wrote in message
              >> news:0F7Lf.5940 4$mf2.36568@new sfe6-win.ntli.net...[color=darkred]
              >>> Tony Marston said the following on 21/02/2006 09:46:
              >>>> Don't be silly. The execution path is sequential, so after a command
              >>>> which does not involve a jump is executed the next instruction to be
              >>>> executed will be the very next instruction in the same object method.
              >>>> If you have just erased the object that contains the next instruction
              >>>> what do you think will happen? How is the PHP processor supposed to
              >>>> know where to go?
              >>>>
              >>> I dunno about PHP, but the equivalent is perfectly possible in C++, i.e.
              >>> you can call delete on this (assuming it's a heap-based object).
              >>>
              >>> The object and its method code are not one and the same thing. Deleting
              >>> an object doesn't mean that the code disappears...[/color]
              >>
              >> An object is comprised of methods (code) and properties (variables). If
              >> you delete/unset an object then both disappear as all their reference
              >> points no longer exist. The code may still exist in the class definition,
              >> but the object, which contains a copy of that code in memory, does not,
              >> so how can it continue executing any of that code?[/color]
              >
              > Well, in C++, objects most definitely don't keep a copy of the method code
              > in memory. When you call something like obj.Func(var); in C++ (assuming
              > obj is an instance of class Foo), the compiler actually translates that to
              > something like:
              >
              > __Foo__Func(&ob j, var);
              >
              > with the method actually just equivalent to a normal global function,
              > internally defined as something like:
              >
              > void __Foo__Func(str uct Foo *this, int var)
              > {
              > ...
              > }
              >
              > and Foo internally defined as:
              >
              > struct Foo
              > {
              > /* member variables of Foo */
              > }
              >
              > Calling delete this; just deallocates the storage space set aside for the
              > Foo struct. Nothing at all happens to the code.
              >
              > I'd like to think that something similar occurs in PHP, as making a "copy"
              > of the code every time you create an object would be a waste of time and
              > memory.
              >
              >[color=green]
              >> Deleting an object while you are still inside it is like blowing up a
              >> house when you are still inside - not a good idea![/color]
              >
              > Going back to C++, use of "delete this" is quite a common practice in
              > smart-pointer/reference-counting classes, or objects designed not to have
              > any other references to them.
              >
              >
              > --
              > Oli[/color]


              Comment

              • Oli Filth

                #8
                Re: unset($this) or maybe unset(&amp;$thi s)

                Tony Marston said the following on 23/02/2006 23:49:[color=blue]
                > PHP is not C++, so expecting the same behaviour from two different languages
                > is just being too optimistic.[/color]

                I'm not; I was just disputing the assertion that an object is a
                collection of code and variables.

                [color=blue]
                > It is not wise to delete an object while you
                > are still inside it, nor is it wise to overwrite it with something else
                > while you are still inside it. Why? Because it buggers up any references to
                > the original object which may still exist in other places.[/color]

                In most situations, yes. But the whole point of having an object delete
                itself (in OO languages in general) is in situations where there are
                intentionally no external pointers/references to it. In PHP, I can't
                see how such a situation could exist, though.

                The point is moot, however, because the language doesn't allow it...


                --
                Oli

                Comment

                Working...