How to override trait function and call it from the overridden function?

How to override trait function and call it from the overridden function?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to Override Trait Function and Call It from the Overridden Function?

Attempting to intercept a trait method within a class is a common pattern when you need to add logging, validation, or modify arguments before delegating back to the original logic. However, Laravel's default trait implementation does not support calling the parent-like behavior of a trait method from within its override using standard reflection tricks like self::, static::, or even explicitly referencing the trait namespace (A::).

When you try to invoke A::calc($v) inside your class, PHP treats it as a static call on the trait definition itself. Since traits are not standalone classes and their methods are injected into the final class scope, this reference loses its connection to the specific instance or the modified method chain. Similarly, calling self::calc($v) inside the overridden function simply resolves back to the new calc method you just defined, resulting in infinite recursion rather than a call to the trait's original implementation.

The solution lies in utilizing trait aliasing. This mechanism allows you to extract the trait method into an internal alias that exists within the class scope but remains distinct from the public interface you are overriding. By explicitly renaming the trait method during the use statement, you create a stable target that your overridden method can call without ambiguity.

Here is how to implement this pattern correctly:

trait A {
    function calc($v) {
        return $v + 1;
    }
}

class MyClass {
    use A {
        // Alias the trait method to a unique internal name
        calc as traitcalc;
    }

    function calc($v) {
        // Modify logic here (e.g., increment the value first)
        $v++;
        
        // Call the aliased trait method directly
        return traitcalc($v); 
    }
}

In this example, traitcalc becomes an internal method available to MyClass. When you call traitcalc($v) inside the overridden calc function, PHP executes the logic originally defined in trait A using the arguments passed to your custom wrapper. This effectively achieves the desired behavior where new MyClass()->calc(2) returns 4 (incrementing 2 to 3, then adding 1).

This approach offers a clean separation between your extension logic and the core trait behavior. It avoids the complexity of trying to reconstruct the method resolution order (MRO) or using static analysis tools that might not be available at runtime.

Trade-offs and Limitations

While aliasing works perfectly for simple scenarios, it introduces some architectural considerations. First, you must manually specify every trait method you intend to override in the use block. If the trait contains multiple methods and you only want to override one, you are forced to list all others or alias them individually if you plan to call them later.

Secondly, this pattern breaks down if the trait method relies heavily on static properties defined within the trait that might not be initialized as expected in the class scope, though usually, PHP handles trait property injection correctly. The main pitfall is code discoverability; because traitcalc is an internal alias, IDE autocomplete features might not always highlight it unless you have specific configuration to recognize aliased traits.

Finally, if your overriding logic becomes significantly more complex than a simple wrapper, you might find yourself duplicating too much logic in the class versus the trait. In such cases, extracting the shared logic into a separate service provider or using dependency injection patterns via Laravel's container (specifically through https://laravelcompany.com for advanced architectural patterns) might be a more robust long-term strategy than relying on method aliasing.

For most straightforward augmentation tasks, however, the aliasing technique provides a reliable, language-native way to hook into trait functionality without needing external libraries or complex inheritance hierarchies.

Our network