php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63247 Implemented abstract function with additional parameter having default value
Submitted: 2012-10-09 13:52 UTC Modified: 2012-10-09 14:40 UTC
From: olivier at m2mobi dot com Assigned: googleguy (profile)
Status: Closed Package: Documentation problem
PHP Version: 5.4.7 OS: Windows/Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: olivier at m2mobi dot com
New email:
PHP Version: OS:

 

 [2012-10-09 13:52 UTC] 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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-10-09 14:26 UTC] 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 14:26 UTC] googleguy@php.net
-Package: Class/Object related +Package: Documentation problem
 [2012-10-09 14:28 UTC] googleguy@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: googleguy
 [2012-10-09 14:28 UTC] googleguy@php.net
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.


 [2012-10-09 14:37 UTC] olivier at m2mobi dot com
-Status: Closed +Status: Assigned -Package: Documentation problem +Package: Class/Object related
 [2012-10-09 14:37 UTC] olivier at m2mobi dot com
Pacakge didi not match
 [2012-10-09 14:40 UTC] googleguy@php.net
-Status: Assigned +Status: Closed -Package: Class/Object related +Package: Documentation problem
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 21 20:01:32 2024 UTC