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

Your name:

Reply:


Posted by Mortimer on 07/19/07 10:26

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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация