php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43499 Inproper use of extending class method from extended class.
Submitted: 2007-12-04 22:36 UTC Modified: 2007-12-07 11:00 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: bholbrook at servillian dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.2.5 OS: Linux Cent OS
Private report: No CVE-ID: None
 [2007-12-04 22:36 UTC] bholbrook at servillian dot com
Description:
------------
In the example, class A gains a knowledge of it's extending classes functions. By allowing the call from the entended class to an extending class method, the extended class method becomes unavailable to any other extending class.

The only time this makes sense is if class A were an abstract class and defined method3 as an abstract function.

Reproduce code:
---------------
<?php
class A {
	public function method1(){
		echo "I am method A::method1<br/>";
		$this->method3();
	}
}
class B extends A{
	public function method2(){
		echo "I am method B::method2<br/>";
		$this->method1();
	}
	public function method3(){
		echo "I am method B::method3<br/>";
	}
}
class C extends A{
	public function method4(){
		echo "I am method C::method4<br/>";
		$this->method1();
	}
}
$oB = new B();
$oB->method2();
$oC = new C();
$oC->method4();
?>

Expected result:
----------------
The expected result of $oB->method2(); is the current results of $oC->method4();

Actual result:
--------------
Currently, $oB->method2() calls A::method1() (correct) which in turn calls B::method3() (incorrect).

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-12-05 10:59 UTC] jani@php.net
Output: 

I am method B::method2
I am method A::method1
I am method B::method3
I am method C::method4
I am method A::method1

Fatal error: Call to undefined method C::method3() in /home/jani/t.php on line 6

Exactly what is wrong in this? AFAICT, it works exactly how it's supposed to work.
 [2007-12-05 16:41 UTC] bholbrook at servillian dot com
$oB->method2() should work, but the call $this->method3() from A::method1() should cause the same fatal error seen when calling it through $oC->method4();

Class A should have no knowledge of it's extending classes methods.
 [2007-12-07 03:08 UTC] crrodriguez at suse dot de
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php


http://php.net/manual/en/language.oop5.visibility.php

"Public declared items can be accessed **everywhere**".. and you have an  instance of C class that extends A , so it is the expected behaviuor..
 [2007-12-07 11:00 UTC] jani@php.net
See above.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 10 12:01:33 2025 UTC