PHP › Traits

What happens when a single class uses multiple traits that happen to define a method with the exact same name, and how can this naming conflict be resolved?

Intermediate

Answer

When a class uses two or more traits that each define a method sharing the exact same name, PHP raises a fatal error since it cannot automatically determine which of the conflicting methods should actually be used, and this conflict can be explicitly resolved using the insteadof keyword to specify which trait's version of the method should take precedence, optionally combined with the as keyword to also make the other trait's conflicting method accessible under a different alias name if it is still needed.
class Report {
    use Loggable, Auditable {
        Loggable::log insteadof Auditable;
        Auditable::log as auditLog;
    }
}
Real-world example A Report class uses two separate traits that both happen to define a method called log, and explicitly resolves the naming conflict by specifying that Loggable's version should be used by default, while still keeping Auditable's version accessible under the alias auditLog.

Common follow-ups: What happens if this kind of naming conflict is never explicitly resolved at all?;Can this same conflict resolution syntax also be used to change a method's visibility?