php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51504 Incorrect Inheritence Order Within The Context Of The Class
Submitted: 2010-04-08 04:34 UTC Modified: 2010-04-08 05:58 UTC
From: phoenix at twistersfury dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.3.2 OS: Windows XP Pro w/IIS
Private report: No CVE-ID: None
 [2010-04-08 04:34 UTC] 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:

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-04-08 05:30 UTC] rasmus@php.net
-Status: Open +Status: Bogus
 [2010-04-08 05:30 UTC] rasmus@php.net
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 05:45 UTC] 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:58 UTC] rasmus@php.net
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 06:10 UTC] phoenix at twistersfury dot com
Ahh. Thanks for the info.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon May 20 21:01:34 2024 UTC