Bug #63247 [Opn]: Implemented abstract function with additional parameter having default value

From: Date: Tue, 09 Oct 2012 14:26:49 +0000
Subject: Bug #63247 [Opn]: Implemented abstract function with additional parameter having default value
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-8948@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=63247&edit=1 ID: 63247 Updated by: googleguy@php.net Reported by: olivier at m2mobi dot com Summary: Implemented abstract function with additional parameter having default value Status: Open Type: Bug -Package: Class/Object related +Package: Documentation problem Operating System: Windows/Linux PHP Version: 5.4.7 Block user comment: N Private report: N New Comment: This is actually a documentation problem not a bug. The documentation states "Furthermore the signatures of the methods must match, i.e. the type hints and the number of required arguments must be the same.", which means that any number of optional arguments are allowed in the child method's signature. This is what's happening here. The second argument is options so it doesn't actually conflict with the documented behavior. However, I too did not catch this at first glance. So I've expanded on the docs for abstract to elaborate on this a bit and added an example in the documentation demonstrating this feature. That should help clarify in the future :) Previous Comments: ------------------------------------------------------------------------ [2012-10-09 13:52:25] olivier at m2mobi dot com Description: ------------ Implemented abstract function with additional parameter having default value doesn't cause any error. Will output: I am the first arg. I am the additional arg. I am the first arg. I am the additional arg default value. If no default value the script crash. Test script: --------------- <?php abstract class AbstractMother { public abstract function methodWithOneArg( $arg ); } class ConcreteChild extends AbstractMother { public function methodWithOneArg($arg, $arg2="I am the additional arg default value.") { echo $arg." ".$arg2 ; } } $child = new ConcreteChild() ; $child->methodWithOneArg( "I am the first arg.", "I am the additional arg.") ; echo "<br>" ; $child->methodWithOneArg("I am the first arg.") ; Expected result: ---------------- FATAL ERROR thrown Actual result: -------------- Just works, addition of a third argument is ignored: <?php class ConcreteChild2 extends AbstractMother { public function methodWithOneArg($arg, $arg2="I am the additional arg default value.", $arg3="I am another arg default value.") { echo $arg." ".$arg2." ".$arg3 ; } } echo "<br>" ;echo "<br>" ; $child2 = new ConcreteChild2() ; $child->methodWithOneArg( "I am the first arg.", "I am the additional arg.", "I am the other additional arg") ; echo "<br>" ; $child->methodWithOneArg( "I am the first arg.", "I am the additional arg.") ; echo "<br>" ; $child->methodWithOneArg("I am the first arg.") ; ?> Will output: I am the first arg. I am the additional arg. I am the first arg. I am the additional arg. I am the first arg. I am the additional arg default value. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=63247&edit=1

« previous php.doc.bugs (#8948) next »