Skip to content
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

Fix GH-13177: PHP 8.3.2: final private constructor not allowed when used in trait #13179

Closed
wants to merge 2 commits into from

Conversation

nielsdos
Copy link
Member

zend_compile has an exception to this rule for constructors using zend_is_constructor, which compares the function name to __construct. Sadly, zend_is_constructor is not a public API, but we can just do the string compare ourselves.

…n used in trait

zend_compile has an exception to this rule for constructors using
`zend_is_constructor`, which compares the function name to
`__construct`. Sadly, `zend_is_constructor` is not a public API, but we
can just do the string compare ourselves.
Copy link
Member

@iluuu1994 iluuu1994 left a comment

Choose a reason for hiding this comment

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

trait Foo {
    private final function test() {}
}

class Bar {
    use Foo { test as __construct; }
}

class Baz extends Bar {
    public function __construct() {}
}

This emits a warning for Bar when it probably shouldn't. I'm not sure this is worth fixing, since Foo itself is already dubious.

The change itself LGTM.

@nielsdos
Copy link
Member Author

This emits a warning for Bar when it probably shouldn't. I'm not sure this is worth fixing, since Foo itself is already dubious.

Let's see. In this case (with this PR) we get 2 warnings:
One for the definition in the trait.
Followed by one because test is also added to Bar. So the warning is not for __construct, but for the additional test method.
But indeed the one for test in Bar is redundant.

I pushed a fix for this to this PR, now only one warning is emitted.

@iluuu1994
Copy link
Member

@nielsdos Oh, I wasn't aware that with as the method will be available via both symbols...

@nielsdos
Copy link
Member Author

Yeah, I also used to not know this until I heard the feature is called trait aliasing, which makes sense then why it behaves that way 🙂
Anyway, the behaviour of your code snippet is now the same as it was prior to PHP 8.3: only a warning for the definition. So from a BC perspective I think the change right now makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PHP 8.3.2: final private constructor not allowed when used in trait
2 participants