Bug #63247 [Opn->Csd]: Implemented abstract function with additional parameter having default value
| From: | googleguy@php.net | Date: | Tue, 09 Oct 2012 14:28:28 +0000 |
| Subject: | Bug #63247 [Opn->Csd]: Implemented abstract function with additional parameter having default value | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-8949@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
+Status: Closed
Type: Bug
Package: Documentation problem
Operating System: Windows/Linux
PHP Version: 5.4.7
-Assigned To:
+Assigned To: googleguy
Block user comment: N
Private report: N
New Comment:
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.
Thank you for the report, and for helping us make our documentation better.
Previous Comments:
------------------------------------------------------------------------
[2012-10-09 14:26:49] googleguy@php.net
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 :)
------------------------------------------------------------------------
[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