__destruct() destroy an object that contains a reference to another object

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

    __destruct() destroy an object that contains a reference to another object

    Hi, i'm using PHP 5.1

    I have two objects and the second one is using an instance of the
    first.
    As displayed in the example below, the Garbage Collector calls the
    destruct method of the first class before the second even if the second
    contains a reference to the other, so i can't complete all the
    operations not having all "the code available"!

    Thank you


    Ex.:

    class MyFirstClass
    {
    public function __construct()
    {
    ... operations...
    }

    public function __destruct()
    {
    echo "Destroyed MyFirstClass";
    }
    }



    class MySecondClass
    {

    private $obj = NULL

    public function __construct($ob j)
    {
    ... operations...
    }

    public function __destruct()
    {
    echo "Destroyed MySecondClass";
    }
    }


    $obj1 = new MyFirstClass();

    $obj2 = new MySecondClass($ obj1);

    // Result
    // Destroyed MyFirstClass
    // Destroyed MySecondClass


  • gosha bine

    #2
    Re: __destruct() destroy an object that contains a reference to anotherobject

    On 19.07.2007 09:39 Mortimer wrote:
    Hi, i'm using PHP 5.1
    >
    I have two objects and the second one is using an instance of the first.
    As displayed in the example below, the Garbage Collector calls the
    destruct method of the first class before the second even if the second
    contains a reference to the other, so i can't complete all the
    operations not having all "the code available"!
    >
    [skip]

    class Database {
    function __destruct() {
    echo "Destroyed Database\n";
    }}

    class Session {
    private $db;
    function __construct($db ) {
    $this->db = $db;
    }
    function __destruct() {
    echo "Destroyed Session\n";
    }}

    $sess = new Session(new Database);


    prints for me


    Destroyed Session
    Destroyed Database


    just as one can expect

    what version of php are you using?

    --
    gosha bine

    extended php parser ~ http://code.google.com/p/pihipi
    blok ~ http://www.tagarga.com/blok

    Comment

    • Mortimer

      #3
      Re: __destruct() destroy an object that contains a reference to another object

      gosha bine ci ha detto :
      On 19.07.2007 09:39 Mortimer wrote:
      >Hi, i'm using PHP 5.1
      >>
      >I have two objects and the second one is using an instance of the first.
      >As displayed in the example below, the Garbage Collector calls the
      >destruct method of the first class before the second even if the second
      >contains a reference to the other, so i can't complete all the operations
      >not having all "the code available"!
      >>
      >[skip]
      >
      >
      class Database {
      function __destruct() {
      echo "Destroyed Database\n";
      }}
      >
      class Session {
      private $db;
      function __construct($db ) {
      $this->db = $db;
      }
      function __destruct() {
      echo "Destroyed Session\n";
      }}
      >
      $sess = new Session(new Database);
      >
      >
      prints for me
      >
      >
      Destroyed Session
      Destroyed Database
      >
      >
      just as one can expect
      >
      what version of php are you using?
      Thanks, i'm using "PHP Version 5.1.2"
      Just another question, i have to use the $db object also out of the
      Session class, that's why i create an istance and then pass it to
      Session.

      $obj1 = new DBHandler();
      $obj2 = new SessionHandler( $obj1);

      while you use this syntax

      $obj2 = new SessionHandler( new DBHandler);

      Your syntax works and mine not!! Can you tell me why?

      What's the difference?

      Thank you!


      Comment

      • gosha bine

        #4
        Re: __destruct() destroy an object that contains a reference to anotherobject

        On 19.07.2007 12:18 Mortimer wrote:
        gosha bine ci ha detto :
        >On 19.07.2007 09:39 Mortimer wrote:
        >>Hi, i'm using PHP 5.1
        >>>
        >>I have two objects and the second one is using an instance of the first.
        >>As displayed in the example below, the Garbage Collector calls the
        >>destruct method of the first class before the second even if the
        >>second contains a reference to the other, so i can't complete all the
        >>operations not having all "the code available"!
        >>>
        >>[skip]
        >>
        >>
        >class Database {
        > function __destruct() {
        > echo "Destroyed Database\n";
        >}}
        >>
        >class Session {
        > private $db;
        > function __construct($db ) {
        > $this->db = $db;
        > }
        > function __destruct() {
        > echo "Destroyed Session\n";
        >}}
        >>
        >$sess = new Session(new Database);
        >>
        >>
        >prints for me
        >>
        >>
        >Destroyed Session
        >Destroyed Database
        >>
        >>
        >just as one can expect
        >>
        >what version of php are you using?
        >
        Thanks, i'm using "PHP Version 5.1.2"
        Just another question, i have to use the $db object also out of the
        Session class, that's why i create an istance and then pass it to Session.
        >
        $obj1 = new DBHandler();
        $obj2 = new SessionHandler( $obj1);
        >
        while you use this syntax
        >
        $obj2 = new SessionHandler( new DBHandler);
        >
        Your syntax works and mine not!! Can you tell me why?
        >
        What's the difference?
        >
        Thank you!
        >
        >
        I've noticed in your first example you didn't assign anything to
        MySecondClass->$obj, perhaps that is the problem ;)


        --
        gosha bine

        extended php parser ~ http://code.google.com/p/pihipi
        blok ~ http://www.tagarga.com/blok

        Comment

        • Mortimer

          #5
          Re: __destruct() destroy an object that contains a reference to another object

          Sembra che gosha bine abbia detto :
          On 19.07.2007 12:18 Mortimer wrote:
          >gosha bine ci ha detto :
          >>On 19.07.2007 09:39 Mortimer wrote:
          >>>Hi, i'm using PHP 5.1
          >>>>
          >>>I have two objects and the second one is using an instance of the first.
          >>>As displayed in the example below, the Garbage Collector calls the
          >>>destruct method of the first class before the second even if the second
          >>>contains a reference to the other, so i can't complete all the operations
          >>>not having all "the code available"!
          >>>>
          >>>[skip]
          >>>
          >>>
          >>class Database {
          >> function __destruct() {
          >> echo "Destroyed Database\n";
          >>}}
          >>>
          >>class Session {
          >> private $db;
          >> function __construct($db ) {
          >> $this->db = $db;
          >> }
          >> function __destruct() {
          >> echo "Destroyed Session\n";
          >>}}
          >>>
          >>$sess = new Session(new Database);
          >>>
          >>>
          >>prints for me
          >>>
          >>>
          >>Destroyed Session
          >>Destroyed Database
          >>>
          >>>
          >>just as one can expect
          >>>
          >>what version of php are you using?
          >>
          >Thanks, i'm using "PHP Version 5.1.2"
          >Just another question, i have to use the $db object also out of the Session
          >class, that's why i create an istance and then pass it to Session.
          >>
          >$obj1 = new DBHandler();
          >$obj2 = new SessionHandler( $obj1);
          >>
          >while you use this syntax
          >>
          >$obj2 = new SessionHandler( new DBHandler);
          >>
          >Your syntax works and mine not!! Can you tell me why?
          >>
          >What's the difference?
          >>
          >Thank you!
          >>
          >>
          >
          I've noticed in your first example you didn't assign anything to
          MySecondClass->$obj, perhaps that is the problem ;)
          Sorry, i didn't write it, but i assign the object in the constructor,
          like this:

          class Session {
          private $db;
          function __construct($db ) {
          $this->db = $db;
          }
          function __destruct() {
          echo "Destroyed Session\n";
          }}

          Uhm.. thank you


          Comment

          • gosha bine

            #6
            Re: __destruct() destroy an object that contains a reference to anotherobject

            On 19.07.2007 12:26 Mortimer wrote:
            >I've noticed in your first example you didn't assign anything to
            >MySecondClas s->$obj, perhaps that is the problem ;)
            >
            Sorry, i didn't write it, but i assign the object in the constructor,
            like this:
            >
            class Session {
            private $db;
            function __construct($db ) {
            $this->db = $db;
            }
            function __destruct() {
            echo "Destroyed Session\n";
            }}
            >
            Uhm.. thank you
            >
            >
            I'm lost now. :-o

            What does this print for you?


            class Database {
            function __destruct() {
            echo "Destroyed Database\n";
            }}
            class Session {
            private $db;
            function __construct($db ) {
            $this->db = $db;
            }
            function __destruct() {
            echo "Destroyed Session\n";
            }}
            $db = new Database;
            $sess = new Session($db);
            die();





            --
            gosha bine

            extended php parser ~ http://code.google.com/p/pihipi
            blok ~ http://www.tagarga.com/blok

            Comment

            • Jerry Stuckle

              #7
              Re: __destruct() destroy an object that contains a reference to anotherobject

              Mortimer wrote:
              Hi, i'm using PHP 5.1
              >
              I have two objects and the second one is using an instance of the first.
              As displayed in the example below, the Garbage Collector calls the
              destruct method of the first class before the second even if the second
              contains a reference to the other, so i can't complete all the
              operations not having all "the code available"!
              >
              Thank you
              >
              >
              Ex.:
              >
              class MyFirstClass
              {
              public function __construct()
              {
              ... operations...
              }
              >
              public function __destruct()
              {
              echo "Destroyed MyFirstClass";
              }
              }
              >
              >
              >
              class MySecondClass
              {
              >
              private $obj = NULL
              >
              public function __construct($ob j)
              {
              ... operations...
              }
              >
              public function __destruct()
              {
              echo "Destroyed MySecondClass";
              }
              }
              >
              >
              $obj1 = new MyFirstClass();
              >
              $obj2 = new MySecondClass($ obj1);
              >
              // Result
              // Destroyed MyFirstClass
              // Destroyed MySecondClass
              >
              >
              Hmmm, personally I would consider this to be a bug. I'd suggest you
              report it and see what they say.

              Iván has a valid point when it comes to circular references, but that's
              not the case here. I think PHP should be able to determine which object
              should be destroyed first.

              --
              =============== ===
              Remove the "x" from my email address
              Jerry Stuckle
              JDS Computer Training Corp.
              jstucklex@attgl obal.net
              =============== ===

              Comment

              • Mortimer

                #8
                Re: __destruct() destroy an object that contains a reference to another object

                Scriveva gosha bine giovedì, 19/07/2007:
                On 19.07.2007 12:26 Mortimer wrote:
                >>I've noticed in your first example you didn't assign anything to
                >>MySecondCla ss->$obj, perhaps that is the problem ;)
                >>
                >Sorry, i didn't write it, but i assign the object in the constructor, like
                >this:
                >>
                >class Session {
                > private $db;
                > function __construct($db ) {
                > $this->db = $db;
                > }
                > function __destruct() {
                > echo "Destroyed Session\n";
                >}}
                >>
                >Uhm.. thank you
                >>
                >>
                >
                I'm lost now. :-o
                >
                What does this print for you?
                >
                >
                class Database {
                function __destruct() {
                echo "Destroyed Database\n";
                }}
                class Session {
                private $db;
                function __construct($db ) {
                $this->db = $db;
                }
                function __destruct() {
                echo "Destroyed Session\n";
                }}
                $db = new Database;
                $sess = new Session($db);
                die();
                This prints
                Destroyed Database
                Destroyed Session

                :\


                Comment

                • Mortimer

                  #9
                  Re: __destruct() destroy an object that contains a reference to another object

                  Scriveva Jerry Stuckle giovedì, 19/07/2007:
                  Mortimer wrote:
                  >Hi, i'm using PHP 5.1
                  >>
                  >I have two objects and the second one is using an instance of the first.
                  >As displayed in the example below, the Garbage Collector calls the destruct
                  >method of the first class before the second even if the second contains a
                  >reference to the other, so i can't complete all the operations not having
                  >all "the code available"!
                  >>
                  >Thank you
                  >>
                  >>
                  >Ex.:
                  >>
                  >class MyFirstClass
                  >{
                  > public function __construct()
                  > {
                  > ... operations...
                  > }
                  >>
                  > public function __destruct()
                  > {
                  > echo "Destroyed MyFirstClass";
                  > }
                  >}
                  >>
                  >>
                  >>
                  >class MySecondClass
                  >{
                  >>
                  > private $obj = NULL
                  >>
                  > public function __construct($ob j)
                  > {
                  > ... operations...
                  > }
                  >>
                  > public function __destruct()
                  > {
                  > echo "Destroyed MySecondClass";
                  > }
                  >}
                  >>
                  >>
                  >$obj1 = new MyFirstClass();
                  >>
                  >$obj2 = new MySecondClass($ obj1);
                  >>
                  >// Result
                  >// Destroyed MyFirstClass
                  >// Destroyed MySecondClass
                  >>
                  >>
                  >
                  Hmmm, personally I would consider this to be a bug. I'd suggest you report
                  it and see what they say.
                  >
                  Iván has a valid point when it comes to circular references, but that's not
                  the case here. I think PHP should be able to determine which object should
                  be destroyed first.
                  Uhm.. ok.. do you mean in the php.net site or in a Newsgroup?


                  Comment

                  • gosha bine

                    #10
                    Re: __destruct() destroy an object that contains a reference to anotherobject

                    On 19.07.2007 13:54 Mortimer wrote:
                    Scriveva gosha bine giovedì, 19/07/2007:
                    >On 19.07.2007 12:26 Mortimer wrote:
                    >>>I've noticed in your first example you didn't assign anything to
                    >>>MySecondClas s->$obj, perhaps that is the problem ;)
                    >>>
                    >>Sorry, i didn't write it, but i assign the object in the constructor,
                    >>like this:
                    >>>
                    >>class Session {
                    >> private $db;
                    >> function __construct($db ) {
                    >> $this->db = $db;
                    >> }
                    >> function __destruct() {
                    >> echo "Destroyed Session\n";
                    >>}}
                    >>>
                    >>Uhm.. thank you
                    >>>
                    >>>
                    >>
                    >I'm lost now. :-o
                    >>
                    >What does this print for you?
                    >>
                    >>
                    >class Database {
                    > function __destruct() {
                    > echo "Destroyed Database\n";
                    >}}
                    >class Session {
                    > private $db;
                    > function __construct($db ) {
                    > $this->db = $db;
                    > }
                    > function __destruct() {
                    > echo "Destroyed Session\n";
                    >}}
                    >$db = new Database;
                    >$sess = new Session($db);
                    >die();
                    >
                    This prints
                    Destroyed Database
                    Destroyed Session
                    >
                    :\
                    >
                    >
                    Definitely a bug. I would upgrade to php 5.2.





                    --
                    gosha bine

                    extended php parser ~ http://code.google.com/p/pihipi
                    blok ~ http://www.tagarga.com/blok

                    Comment

                    • Jerry Stuckle

                      #11
                      Re: __destruct() destroy an object that contains a reference to anotherobject

                      Mortimer wrote:
                      Scriveva Jerry Stuckle giovedì, 19/07/2007:
                      >Mortimer wrote:
                      >>Hi, i'm using PHP 5.1
                      >>>
                      >>I have two objects and the second one is using an instance of the first.
                      >>As displayed in the example below, the Garbage Collector calls the
                      >>destruct method of the first class before the second even if the
                      >>second contains a reference to the other, so i can't complete all the
                      >>operations not having all "the code available"!
                      >>>
                      >>Thank you
                      >>>
                      >>>
                      >>Ex.:
                      >>>
                      >>class MyFirstClass
                      >>{
                      >> public function __construct()
                      >> {
                      >> ... operations...
                      >> }
                      >>>
                      >> public function __destruct()
                      >> {
                      >> echo "Destroyed MyFirstClass";
                      >> }
                      >>}
                      >>>
                      >>>
                      >>>
                      >>class MySecondClass
                      >>{
                      >>>
                      >> private $obj = NULL
                      >>>
                      >> public function __construct($ob j)
                      >> {
                      >> ... operations...
                      >> }
                      >>>
                      >> public function __destruct()
                      >> {
                      >> echo "Destroyed MySecondClass";
                      >> }
                      >>}
                      >>>
                      >>>
                      >>$obj1 = new MyFirstClass();
                      >>>
                      >>$obj2 = new MySecondClass($ obj1);
                      >>>
                      >>// Result
                      >>// Destroyed MyFirstClass
                      >>// Destroyed MySecondClass
                      >>>
                      >>>
                      >>
                      >Hmmm, personally I would consider this to be a bug. I'd suggest you
                      >report it and see what they say.
                      >>
                      >Iván has a valid point when it comes to circular references, but
                      >that's not the case here. I think PHP should be able to determine
                      >which object should be destroyed first.
                      >
                      Uhm.. ok.. do you mean in the php.net site or in a Newsgroup?
                      >
                      >
                      The php.net site. Posting a bug report here won't get it fixed; this is
                      not the official bug reporting location.

                      --
                      =============== ===
                      Remove the "x" from my email address
                      Jerry Stuckle
                      JDS Computer Training Corp.
                      jstucklex@attgl obal.net
                      =============== ===

                      Comment

                      • Mortimer

                        #12
                        Re: __destruct() destroy an object that contains a reference to another object

                        Jerry Stuckle ha spiegato il 19/07/2007 :
                        Mortimer wrote:
                        >Scriveva Jerry Stuckle giovedì, 19/07/2007:
                        >>Mortimer wrote:
                        >>>Hi, i'm using PHP 5.1
                        >>>>
                        >>>I have two objects and the second one is using an instance of the first.
                        >>>As displayed in the example below, the Garbage Collector calls the
                        >>>destruct method of the first class before the second even if the second
                        >>>contains a reference to the other, so i can't complete all the operations
                        >>>not having all "the code available"!
                        >>>>
                        >>>Thank you
                        >>>>
                        >>>>
                        >>>Ex.:
                        >>>>
                        >>>class MyFirstClass
                        >>>{
                        >>> public function __construct()
                        >>> {
                        >>> ... operations...
                        >>> }
                        >>>>
                        >>> public function __destruct()
                        >>> {
                        >>> echo "Destroyed MyFirstClass";
                        >>> }
                        >>>}
                        >>>>
                        >>>>
                        >>>>
                        >>>class MySecondClass
                        >>>{
                        >>>>
                        >>> private $obj = NULL
                        >>>>
                        >>> public function __construct($ob j)
                        >>> {
                        >>> ... operations...
                        >>> }
                        >>>>
                        >>> public function __destruct()
                        >>> {
                        >>> echo "Destroyed MySecondClass";
                        >>> }
                        >>>}
                        >>>>
                        >>>>
                        >>>$obj1 = new MyFirstClass();
                        >>>>
                        >>>$obj2 = new MySecondClass($ obj1);
                        >>>>
                        >>>// Result
                        >>>// Destroyed MyFirstClass
                        >>>// Destroyed MySecondClass
                        >>>>
                        >>>>
                        >>>
                        >>Hmmm, personally I would consider this to be a bug. I'd suggest you
                        >>report it and see what they say.
                        >>>
                        >>Iván has a valid point when it comes to circular references, but that's
                        >>not the case here. I think PHP should be able to determine which object
                        >>should be destroyed first.
                        >>
                        >Uhm.. ok.. do you mean in the php.net site or in a Newsgroup?
                        >>
                        >>
                        >
                        The php.net site. Posting a bug report here won't get it fixed; this is not
                        the official bug reporting location.
                        Ok thank you for your support!


                        Comment

                        • Mortimer

                          #13
                          Re: __destruct() destroy an object that contains a reference to another object

                          Il 19/07/2007, Mortimer ha detto :
                          Scriveva gosha bine giovedì, 19/07/2007:
                          >On 19.07.2007 12:26 Mortimer wrote:
                          >>>I've noticed in your first example you didn't assign anything to
                          >>>MySecondClas s->$obj, perhaps that is the problem ;)
                          >>>
                          >>Sorry, i didn't write it, but i assign the object in the constructor, like
                          >>this:
                          >>>
                          >>class Session {
                          >> private $db;
                          >> function __construct($db ) {
                          >> $this->db = $db;
                          >> }
                          >> function __destruct() {
                          >> echo "Destroyed Session\n";
                          >>}}
                          >>>
                          >>Uhm.. thank you
                          >>>
                          >>>
                          >>
                          >I'm lost now. :-o
                          >>
                          >What does this print for you?
                          >>
                          >>
                          >class Database {
                          > function __destruct() {
                          > echo "Destroyed Database\n";
                          >}}
                          >class Session {
                          > private $db;
                          > function __construct($db ) {
                          > $this->db = $db;
                          > }
                          > function __destruct() {
                          > echo "Destroyed Session\n";
                          >}}
                          >$db = new Database;
                          >$sess = new Session($db);
                          >die();
                          >
                          This prints
                          Destroyed Database
                          Destroyed Session
                          >
                          :\
                          So this doesn't work:
                          ----------------------------------------------------
                          try
                          {
                          $db = new DBHandler($arrD ataConn, DB);
                          }
                          catch(Exception $e)
                          {
                          exit("Service not available");
                          }

                          $sess = new DBSession($db);
                          ----------------------------------------------------


                          While this seems to work fine:
                          ----------------------------------------------------
                          $sess = new DBSession($db = new DBHandler($data Conn, DB))
                          ----------------------------------------------------

                          So i can use $db object after the instance of $sess;
                          I'll report this to the php.net site as a bug


                          Comment

                          • Mortimer

                            #14
                            Re: __destruct() destroy an object that contains a reference to another object

                            Mortimer ci ha detto :
                            Hi, i'm using PHP 5.1
                            >
                            I have two objects and the second one is using an instance of the first.
                            As displayed in the example below, the Garbage Collector calls the destruct
                            method of the first class before the second even if the second contains a
                            reference to the other, so i can't complete all the operations not having all
                            "the code available"!
                            >
                            Thank you
                            >
                            >
                            Ex.:
                            >
                            class MyFirstClass
                            {
                            public function __construct()
                            {
                            ... operations...
                            }
                            >
                            public function __destruct()
                            {
                            echo "Destroyed MyFirstClass";
                            }
                            }
                            >
                            >
                            >
                            class MySecondClass
                            {
                            >
                            private $obj = NULL
                            >
                            public function __construct($ob j)
                            {
                            ... operations...
                            }
                            >
                            public function __destruct()
                            {
                            echo "Destroyed MySecondClass";
                            }
                            }
                            >
                            >
                            $obj1 = new MyFirstClass();
                            >
                            $obj2 = new MySecondClass($ obj1);
                            >
                            // Result
                            // Destroyed MyFirstClass
                            // Destroyed MySecondClass
                            I've found this:




                            Comment

                            Working...