Skip to content

fix: adding optional parameters to contructor#987

Merged
Ocramius merged 5 commits intoRoave:8.19.xfrom
mstegmeyer:fix/adding-optional-parameters-to-constructor
Feb 13, 2026
Merged

fix: adding optional parameters to contructor#987
Ocramius merged 5 commits intoRoave:8.19.xfrom
mstegmeyer:fix/adding-optional-parameters-to-constructor

Conversation

@mstegmeyer
Copy link
Copy Markdown
Contributor

Adding optional parameters to a constructor is not a breaking change, since child classes do not need to match the signature of the parent. Only non-optional parameters (or parameters with default values before the last required parameter) are a breaking change in a constructor, as here the method call would need to be adjusted

Small example

class A {
    public function __construct(int $one, int $two = 666) {}
}

class B extends A {
    public function __construct(string $whatever) {
        parent::__construct(1);
    }
}

new B('foo');
new A(2);

Adding the parameter $two in class A here does not have an effect on neither constructing A nor B.

You can try it out here: https://3v4l.org/QYsC8

fixes #978

Comment thread src/DetectChanges/BCBreak/MethodBased/MethodParameterAdded.php Outdated
@Ocramius Ocramius added the bug label Feb 13, 2026
@Ocramius Ocramius added this to the 8.19.0 milestone Feb 13, 2026
Copy link
Copy Markdown
Member

@Ocramius Ocramius left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @mstegmeyer!

@Ocramius Ocramius merged commit 810eb88 into Roave:8.19.x Feb 13, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

False-positive when adding optional parameter to constructor

2 participants