php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48984 Strict errors when implementing an interface (with private methods)
Submitted: 2009-07-20 10:01 UTC Modified: 2009-07-22 15:34 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: themastersleader at hotmail dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.3.0 OS: Win XP
Private report: No CVE-ID: None
 [2009-07-20 10:01 UTC] themastersleader at hotmail dot com
Description:
------------
When we implement an interface,
And we have a base class with a private function,
We can't use that name anymore for a public function in classes that extend the base class.

When we remove the implements everything works fine.

Reproduce code:
---------------
<?php 
error_reporting( E_ALL );
set_error_handler('errorReporting');

interface test { }

class ParentClass
implements test
{
	private function foo() { }
}

class ChildClass
extends ParentClass
{
	public function foo(array $content) { }
}

function errorReporting($errno, $errstr, $errfile, $errline)
{
	echo($errstr . '<br />');
}
?>

Expected result:
----------------
No error messages

Actual result:
--------------
Declaration of ChildClass::foo() should be compatible with that of ParentClass::foo()

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-07-20 10:24 UTC] themastersleader at hotmail dot com
I noticed when the functions have the same params, everything works fine.)
 [2009-07-20 10:56 UTC] jani@php.net
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


 [2009-07-20 11:07 UTC] themastersleader at hotmail dot com
I don't understand why this isn't a bug,

When i remove the interface everything works fine,
So by implementing an interface, we say "Implement the same parameters of the private methods that are defined in the base class, with exception that the access modifier can be different".

This isn't normal behaviour.

Greets
Sander)
 [2009-07-21 11:39 UTC] themastersleader at hotmail dot com
Can someone explain why it isn't a bug?
How to work arround this?
Don't use interfaces isn't a real solution.

Thanks

Sander)
 [2009-07-22 15:34 UTC] themastersleader at hotmail dot com
I think it is a bug, someone else tested it in php 5.2.9 and then it worked, 5.3.0 gives problems.

Greets)
 [2010-07-14 20:18 UTC] itay dot malimovka at gmail dot com
I am having a similar issue in php 5.3 with two classes inheriting from each other. Both have same name methods with different params. Both methods are private. I get the same error.
This is NOT a normal behaviour!
 [2010-07-15 09:25 UTC] giorgio dot liscio at email dot it
this is not a bug

extending a method means that in the sub-class the overrided method can be used as can i use it in the parent class

so

if i have

function test($a, $b, $c){}

in the subclasses every additional parameter must be "not required" to make the method working

function test($a, $b, $c, $d=NULL, $e=NULL, $f=NULL)

=NULL means that are not required


so in your code

class ChildClass extends ParentClass
{
	public function foo(array $content=NULL) { }
}
 [2010-07-15 11:20 UTC] themastersleader at hotmail dot com
Quote "extending a method means that in the sub-class the overrided method can be used as can i use it in the parent class"

The problem is that the sub-class isn't extending the method this because the method is defined as private.
 [2010-07-15 15:16 UTC] itay dot malimovka at gmail dot com
@giorgio dot liscio at email dot it

This is **ONLY** true when the method is protected or public.
 [2010-07-15 19:15 UTC] giorgio dot liscio at email dot it
a private method is exactly like the others... explained by the fact that a private method can be public in a subclass

class A{private function x(){}}
class B extends A{public function x(){parent::x()}}
 [2010-07-15 20:09 UTC] itay dot malimovka at gmail dot com
"a private method is exactly like the others... explained by the fact that a private method can be public in a subclass"
Nooooo

Here, try this:
class A{private function x(){}}
class B extends A{public function b(){x()}}

as you can see, class b DID NOT inherited function x, it can call it from the parent class though, which is what you showed.

Any way, see discussion here:
http://stackoverflow.com/questions/3258523/is-this-a-php-bug-subclasses-must-declare-private-methods-with-the-same-signatur
 [2010-07-15 20:38 UTC] giorgio dot liscio at email dot it
ok i got it
i'm sorry... a private method cannot obviously public in subclasses -__-

so you are saying that any class can have ---their own method with same name with totally different signature---...

because private methods does not depends by parent ones

ok now i think that you are right

fields works right... here is the proof:

class A
{
     private $test;
     public function __construct(){$this->test = "THIS IS A";}
}

class B extends A
{
     private $test;
	 public function printit(){echo isset($this->test) ?: '$this->test is not set';}
}

$b = new B();
$b->printit();


as you can see every class has its own field with the same name

subclass-field does not inherits parent class's field
 [2010-07-16 10:38 UTC] themastersleader at hotmail dot com
Also the strange thing is, when you remove the empty interface from the base class the script doesn't give an error.
 [2010-07-16 15:19 UTC] itay dot malimovka at gmail dot com
That's actually the correct behaviour. If there is no inheritance, there is no problems.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 10:01:28 2024 UTC