Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP 5.4: why can classes override trait methods with a different signature?

I'm wondering if there is any good reason why this behaviour is possible in the current PHP 5.4 implementation:

trait T {
    public function test(PDO $pdo) {}
}

class C {
    use T;
    public function test(DOMDocument $dom) {}
}

I thought that the fact that a class uses a trait, guaranteed that this class had a specific interface available. But here, if we inadvertently override the trait method for another purpose, we don't even receive a Strict Standards notice, as with classic inheritance.

Is this specifically allowed on purpose? What for?

like image 234
BenMorel Avatar asked Jul 17 '12 11:07

BenMorel


People also ask

What is the difference between a class and a trait in PHP?

The main difference between the Traits and Interfaces in PHP is that the Traits define the actual implementation of each method within each class, so many classes implement the same interface but having different behavior, while traits are just chunks of code injected in a class in PHP.

How do you override a trait?

Overriding a trait function The trait function can be overridden simply by defining a function with the same name in the class.

Can a trait extend a class PHP?

Only classes can be extended. Traits are not classes.

Can traits be inherited PHP?

In PHP, a trait is a way to enable developers to reuse methods of independent classes that exist in different inheritance hierarchies. Simply put, traits allow you to create desirable methods in a class setting, using the trait keyword. You can then inherit this class through the use keyword.


1 Answers

This behavior is documented. From php.net (http://php.net/manual/en/language.oop5.traits.php):

An inherited member from a base class is overridden by a member inserted by a Trait. The precedence order is that members from the current class override Trait methods, which in return override inherited methods.

No reason for notices here.

Edit:

I took a look on some more serious literature to shed some light on this topic :) . Looks like that such behavior is a part of traits' definition. They are ment to work this way. This is from research "Traits: Composable Units of Behavior"(Proceedings of the European Conference on Object-Oriented Programming):

Another property of trait composition is that the composition order is irrelevant, and hence conflicting trait methods must be explicitly disambiguated (cf. section 3.5). Conflicts between methods defined in classes and methods defined by incorporated traits are resolved using the following two precedence rules.

– Class methods take precedence over trait methods.

– Trait methods take precedence over superclass methods. This follows from the flattening property, which states that trait methods behave as if they were defined in the class itself.

You can read more here: http://scg.unibe.ch/archive/papers/Scha03aTraits.pdf

like image 100
Eugene Avatar answered Oct 06 '22 00:10

Eugene