473,382 Members | 1,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

__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($obj)
{
... operations...
}

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

$obj2 = new MySecondClass($obj1);

// Result
// Destroyed MyFirstClass
// Destroyed MySecondClass
Jul 19 '07 #1
13 1952
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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Minkyu Kim | last post by:
Hi. Please check this simple test code. --------------------- class TestA: def __init__(self): print "init TestA" def __del__(self): print "del TestA" def SetEvent(self, event):
30
by: jimjim | last post by:
Hello, This is a simple question for you all, I guess . int main(){ double *g= new double; *g = 9; delete g; cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<" "<<endl; *g =...
8
by: vvenk | last post by:
Hello: I just wrote my first ASP.Net application. It worked fine on my machine and when I put into production, the ASP.Net process reaches 50% quite fast and then the system does not work...
16
by: LP | last post by:
Hello, I am trying to use .NET with Excel. I installed Office 2003 and selected ..NET programming suport option, so it installed all those PIA, as MS sugests. But I can not find a way to destroy...
5
by: Michael Moreno | last post by:
Hello, In a class I have this code: public object Obj; If Obj is a COM object I would like to call in the Dispose() method the following code: ...
3
by: Matt B | last post by:
Subject is probably poorly worded, but bear with me as I describe the situation that has me puzzled. I create "Save" objects from a Save "factory" class. Many different windows access this...
4
by: Juergen-Bernhard Adler | last post by:
Hello, pretend some noob has (in a fake-static class) provided the following method public static kill_object($obj) { if (!is_object($obj)) return false;
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
6
by: itsraghz | last post by:
Dear All, I have an issue with destroy() method of java.lang.Process class. All what I am trying to do is, controlling the execution of one program through another. Let's say, Program B has to be...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.