Edit report at http://bugs.php.net/bug.php?id=51504&edit=1
ID: 51504
Comment by: phoenix at twistersfury dot com
Reported by: phoenix at twistersfury dot com
Summary: Incorrect Inheritence Order Within The Context Of The
Class
Status: Bogus
Type: Bug
Package: Class/Object related
Operating System: Windows XP Pro w/IIS
PHP Version: 5.3.2
New Comment:
Ahh. Thanks for the info.
Previous Comments:
------------------------------------------------------------------------
[2010-04-08 05:58:12] [email protected]
The foo() constructor is only called for class foo if you don't have a
__construct() method defined in it, so the other way to fix your problem
is to
simply add a constructor.
------------------------------------------------------------------------
[2010-04-08 05:45:46] phoenix at twistersfury dot com
Ahh...I forgot about that. I got into PHP after the __construct
function. This can be shutdown then.
------------------------------------------------------------------------
[2010-04-08 05:30:47] [email protected]
There is no bug here. You are getting confused by the fact that a
method with
the same name as the class is taken to be the constructor for that
class. Rename
your class to xxx and you will get your expected output.
------------------------------------------------------------------------
[2010-04-08 04:34:01] phoenix at twistersfury dot com
Description:
------------
If you have a child class that overrides a parent class method, and call
that method within the context of the parent, the parent's method is
called first, then the child class. This always happens. If you call the
method outside of the class, then the child's method is called, and the
parent's method is only ran if you tell it to using parent::
If you add a third level of inheritance, under the same conditions, the
parent class will still call first, but the lowest child is called,
skipping those in between.
Adding:
...
class bar2 extends bar {
public function foo() {
echo 'in_child_2:';
}
}
$objClass = new bar();
...
Results in in_parent:in_child_2:
Test script:
---------------
<?php
class foo {
public function foo() {
echo 'in_parent:';
}
public function bar() {
$this->foo();
}
}
class bar extends foo {
public function foo() {
echo 'in_child:';
}
}
$objClass = new bar();
$objClass->bar();
$objClass->foo();
?>
Expected result:
----------------
in_child:in_child:
Actual result:
--------------
in_parent:in_child:in_child:
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=51504&edit=1