Bug #74722 [Nab]: Overridden method with same name as child class

From: Date: Fri, 09 Jun 2017 11:20:56 +0000
Subject: Bug #74722 [Nab]: Overridden method with same name as child class
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-209446@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=74722&edit=1 ID: 74722 Updated by: requinix@php.net Reported by: mb at iname dot com Summary: Overridden method with same name as child class Status: Not a bug Type: Bug Package: *General Issues Operating System: CentOS 6 PHP Version: 7.1.6 Block user comment: N Private report: N New Comment: It does give the deprecated warning. https://3v4l.org/WT1r7 PHP discovers the old-style constructors when it first analyzes the class, and that happens when the file is loaded. No code in the file has been executed at that point so calling error_reporting() in it will not make a difference (for that warning). Best thing is to find your php.ini and change the error_reporting setting. Previous Comments: ------------------------------------------------------------------------ [2017-06-09 11:12:45] mb at iname dot com Thank you, I forgot about PHP4 constructors. However, shouldn't PHP emit E_DEPRECATED notice in this case? Documentation states it should, but it doesn't for me, even if I call error_reporting(E_ALL | E_DEPRECATED); ------------------------------------------------------------------------ [2017-06-09 01:44:19] daverandom@php.net Sorry, but your problem does not imply a bug in PHP itself. For a list of more appropriate places to ask for help using PHP, please visit http://www.php.net/support.php as this bug system is not the appropriate forum for asking support questions. Due to the volume of reports we can not explain in detail here why your report is not a bug. The support channels will be able to provide an explanation for you. Thank you for your interest in PHP. This is only an issue in the case of methods that have the same name as the class, which is treated as a PHP4 (Java-style) constructor. The following works as expected: https://3v4l.org/5CHep See also: https://wiki.php.net/rfc/remove_php4_constructors ------------------------------------------------------------------------ [2017-06-09 01:41:11] mail at pmmaga dot net What you are seeing is the result of your method b() in class b being used as a constructor for it. See: http://php.net/manual/en/language.oop5.decon.php for more info ------------------------------------------------------------------------ [2017-06-09 01:35:27] mb at iname dot com Description: ------------ The example below produces a fatal error: Fatal error: Call to protected b::b() from invalid context in test.php on line 17 Changing visibility of child's method to public results in b::b() being invoked twice (prints 'ChildChild'). Renaming the child class to anything but 'b' resolves the issue. Test script: --------------- <?php class a { protected function b() { echo 'Parent'; } public function test() { $this->b(); } } class b extends a { protected function b() { echo 'Child'; } } $o = new b(); $o->test(); Expected result: ---------------- The script should print "Child". Alternatively, if methods with same name as class are not allowed, the script should fail at parsing stage. Actual result: -------------- Fatal error: Call to protected b::b() from invalid context in test.php on line 17 If b::b() is set public in child, prints 'ChildChild' ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=74722&edit=1

« previous php.bugs (#209446) next »