# Enforcing final in traits

- Thread: https://externals.io/message/60446
- Original archive: https://news-web.php.net/php.internals/60446
- Started by Scott MacVicar on 2012-05-04T18:30:06+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #60446 — Scott MacVicar — 2012-05-04T18:30:06+00:00

URL: https://externals.io/message/60446#60446

This caused a few bugs for us / confusion. The final keyword is accepted inside a trait but it the class also defines a method without the final keyword this takes precedence.

Two solutions:
Enforce final when a trait defines - https://whisky.macvicar.net/patches/0001-If-a-trait-declares-a-method-as-final-enforce-that.patch

Prohibit final form being used in a trait - https://whisky.macvicar.net/patches/002-traits-Disable-use-of-final-keyword.patch

I'm for the first solution here

- S

---

## #60449 — Hannes Magnusson — 2012-05-04T19:46:32+00:00

URL: https://externals.io/message/60446#60449 · In reply to: #60446

> This caused a few bugs for us / confusion. The final keyword is accepted inside a trait but it the class also defines a method without the final keyword this takes precedence.
>

it also ignores visibility abstract and static..

I thought this was intentional?

-Hannes

---

## #60454 — Stefan Marr — 2012-05-04T23:38:18+00:00

URL: https://externals.io/message/60446#60454 · In reply to: #60449

> 
> > […]

The methods in the class always take precedency. 

However, the final keyword might just be something I overlooked.
It sounds sensible that if the final is not explicitly removed during composition to treat it as what final was meant for and check it against the methods defined in the class body as well.

Scott, could you elaborate a bit of the use case of final in a trait?



> it also ignores visibility abstract and static..

abstract is not ignored.
An abstract method in a trait expresses a requirement to the host class (using class).

In general, the ppp visibilities are seen as hints for the standard usage of a trait, but can be changed during composition.



Thanks
Stefan



-- 
Stefan Marr
Software Languages Lab
Vrije Universiteit Brussel
Pleinlaan 2 / B-1050 Brussels / Belgium
http://soft.vub.ac.be/~smarr
Phone: +32 2 629 2974
Fax:   +32 2 629 3525

---

## #60456 — Nikita Popov — 2012-05-05T09:26:47+00:00

URL: https://externals.io/message/60446#60456 · In reply to: #60454

>
> 
>
> > […]
>
> The methods in the class always take precedency.
>
> However, the final keyword might just be something I overlooked.
> It sounds sensible that if the final is not explicitly removed during composition to treat it as what final was meant for and check it against the methods defined in the class body as well.
>
> Scott, could you elaborate a bit of the use case of final in a trait?

Here is an example:
http://stackoverflow.com/questions/7104957/building-a-singleton-trait-with-php-5-4

Not the best things to use traits for, but probably something people
*will* try to do.

Nikita
