-
Notifications
You must be signed in to change notification settings - Fork 254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
remove optional parameter value #582
Conversation
Codecov Report
@@ Coverage Diff @@
## master #582 +/- ##
=========================================
Coverage 95.00% 95.00%
Complexity 110 110
=========================================
Files 11 11
Lines 280 280
=========================================
Hits 266 266
Misses 14 14
Continue to review full report at Codecov.
|
Seems sensible, although with it typed as |
no default value , because you have to pass all parameters |
test with named Argument: class A
{
public string $first;
public ?string $nullableString;
public int $third;
public function __construct(
string $first,
?string $nullableString = null,
int $third,
) {
$this->first = $first;
$this->nullableString = $nullableString;
$this->third = $third;
}
public function dump(){
var_dump($this->first);
var_dump($this->nullableString);
var_dump($this->third);
}
}
$data = new A(
'a string',
third: 20,
);
$data->dump(); current implementation only works in php 8.0 when you use named Argument //result php 8.0
string(8) "a string"
NULL
int(20) //result php 8.1
Deprecated: Optional parameter $nullableString declared before required parameter $third is implicitly treated as a required parameter
Fatal error: Uncaught ArgumentCountError: A::__construct(): Argument #2 ($nullableString) not passed |
extending class test: class A
{
public $p;
public function func(
int $p = null
){
$this->p = $p;
}
}
class B extends A
{
public $p;
public function func(
?int $p = null
) {
$this->p = $p;
}
public function dump(){
var_dump($this->p);
}
}
$data = new B();
$data->func(50);
$data->dump(); php 7.1+
another test: interface AInterface
{
public function __construct(
string $p = null
);
}
class A implements AInterface
{
public $p;
public function __construct(
?string $p = null
) {
$this->p = $p;
}
public function dump(){
var_dump($this->p);
}
}
$data = new A(
'a string'
);
$data->dump(); php 7.1+
|
I changed also the interface
not sure if it is BC
Closes #581