SitePoint Sponsor |
|
User Tag List
Results 1 to 3 of 3
Thread: php, OOP and references
Hybrid View
-
Nov 21, 2004, 07:41 #1
- Join Date
- Feb 2004
- Location
- Ljubljana
- Posts
- 191
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
php, OOP and references
Hi there...
I was playing a bit with PHP5 and I found some strange behaviours or I just
don't understand....
If I have two classes
PHP Code:
class A
{
function __construct()
{
print("A constructor called!<br>");
}
function __destruct()
{
print("A destructor called!<br>");
}
}
class B
{
function __construct()
{
print("B constructor called!<br>");
}
function __destruct()
{
print("B destructor called!<br>");
}
}
PHP Code:$a = new A();
$b = new B();
then I tried with references(although that's depricated in php5)
PHP Code:$a = &new A();
$b = &new B();
but isn't the one of main rules in OOP that objects have to be destroyed in the order they were created!?
so I would expect
A constructor called!
B constructor called!
B destructor called!
A destructor called!
Any thoughts on that!?exit(0);
-
Nov 21, 2004, 10:05 #2
- Join Date
- Nov 2001
- Location
- Bath, UK
- Posts
- 2,498
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by abstraktmedia
You might be thinking of the case where A creates B in its constructor, so when A is destroyed B is also destroyed.
Something like this:
PHP Code:class A {
function __construct() {
print("A constructor called!<br>");
$this->b = new B();
}
function __destruct() {
$this->b = null;
print("A destructor called!<br>");
}
}
class B {
function __construct() {
print("B constructor called!<br>");
}
function __destruct() {
print("B destructor called!<br>");
}
}
$a = new A();
Hello World
-
Nov 21, 2004, 10:17 #3
- Join Date
- Feb 2004
- Location
- Ljubljana
- Posts
- 191
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
hm...interesting....thanx...
exit(0);
Bookmarks