By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,595 Members | 1,294 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,595 IT Pros & Developers. It's quick & easy.

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

P: n/a
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($obj)
{
... operations...
}

public function __destruct()
{
echo "Destroyed MySecondClass";
}
}
$obj1 = new MyFirstClass();

$obj2 = new MySecondClass($obj1);

// Result
// Destroyed MyFirstClass
// Destroyed MySecondClass
Jul 19 '07 #1
Share this Question
Share on Google+
13 Replies


P: n/a
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
Jul 19 '07 #2

P: n/a
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!
Jul 19 '07 #3

P: n/a
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
Jul 19 '07 #4

P: n/a
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
Jul 19 '07 #5

P: n/a
On 19.07.2007 12:26 Mortimer wrote:
>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

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
Jul 19 '07 #6

P: n/a
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($obj)
{
... 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.
js*******@attglobal.net
==================
Jul 19 '07 #7

P: n/a
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
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


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

:\
Jul 19 '07 #8

P: n/a
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($obj)
{
... 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?
Jul 19 '07 #9

P: n/a
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
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


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
Jul 19 '07 #10

P: n/a
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($obj)
{
... 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.
js*******@attglobal.net
==================
Jul 19 '07 #11

P: n/a
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($obj)
{
... 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!
Jul 19 '07 #12

P: n/a
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
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


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($arrDataConn, DB);
}
catch(Exception $e)
{
exit("Service not available");
}

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

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

P: n/a
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($obj)
{
... operations...
}

public function __destruct()
{
echo "Destroyed MySecondClass";
}
}
$obj1 = new MyFirstClass();

$obj2 = new MySecondClass($obj1);

// Result
// Destroyed MyFirstClass
// Destroyed MySecondClass
I've found this:

http://bugs.php.net/bug.php?id=20240
Jul 19 '07 #14

This discussion thread is closed

Replies have been disabled for this discussion.