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

The final modifier to methods coming from traits #4189

Open
mmalferov opened this issue Nov 29, 2024 · 5 comments · May be fixed by #4188
Open

The final modifier to methods coming from traits #4189

mmalferov opened this issue Nov 29, 2024 · 5 comments · May be fixed by #4188

Comments

@mmalferov
Copy link
Member

Can someone explain what the last code example on the Trait page means? This example works down to PHP 5.4.0:

<?php

trait ConstantsTrait
{
    public function method()
    {
        echo 'Hello';
    }
}

class ConstantsExample
{
    use ConstantsTrait;

    final public function method()
    {
        echo 'Hello World';
    }
}

What exactly was allowed to do with traits and the final modifier in PHP 8.3.0?

@alexandre-daubois
Copy link
Contributor

Indeed, I made a mistake in this example. Sorry for that and thanks for spotting it!

The changelog says "The final modifier may now be used when using a method from a trait.". I'm not 100% sure what it means, then... 🤔

@mmalferov
Copy link
Member Author

This is a common documentation problem, or rather the problem of the word use, which seems to say something that actually says literally nothing.

I would forbid using (!)… I would forbid writing (!) this word at all. It is always better to write a specific action instead: call a method, specify a modifier, declare a function, pass a value rather than use a method, use a modifier, use a function, use a value…

"The final modifier may now be used…" — how exactly?! :)

@Girgias
Copy link
Member

Girgias commented Nov 29, 2024

The problem a lot of the times is the people writing the entry in UPGRADING in a way which is kinda useless, so one is just "guessing" what the hell it means.

I think for PHP next I am going to push a migration guide and force people to write the entry in it properly on doc-en instead of minimal text in the UPGRADING file of php-src.

@mmalferov
Copy link
Member Author

mmalferov commented Nov 29, 2024

Take a look at this sample code:

https://3v4l.org/EkGgI#v8.3.0

And try switch to lower PHP version; prior to PHP 8.3.0, You'll get: Fatal error: Cannot use 'final' as method modifier

It seems that it is now clear how the final modifier was allowed to be used.

I have propose changes in the last code example in #4188, please correct it if necessary

@alexandre-daubois
Copy link
Contributor

Great finding, I wasn't aware of as final at all 😄

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

Successfully merging a pull request may close this issue.

3 participants