Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The use statement within a class body is the mechanism by which PHP incorporates a trait, enabling horizontal code reuse. It instructs the PHP compiler to flatten the trait’s methods, properties, and constants directly into the composing class at compile time, effectively bypassing PHP’s single-inheritance limitation.

Basic Syntax

To include a trait, declare the use keyword followed by the trait name inside the class definition. Multiple traits can be imported by separating them with commas.
trait Logger {
    public function log(string $message): void {
        echo $message;
    }
}

trait Authenticator {
    public function authenticate(): bool {
        return true;
    }
}

class User {
    use Logger, Authenticator;
}

Method Precedence

PHP enforces a strict precedence order when resolving method names during trait composition:
  1. Composing Class: Methods defined directly in the class override methods provided by a trait.
  2. Trait: Methods provided by a trait override inherited methods from a parent class.
  3. Parent Class: Inherited methods have the lowest precedence.
class Base {
    public function process(): void {
        echo "Base";
    }
}

trait ProcessorTrait {
    public function process(): void {
        echo "Trait";
    }
}

class Child extends Base {
    use ProcessorTrait;
    
    // If uncommented, this overrides the trait method.
    // public function process(): void { echo "Child"; }
}

Method Conflict Resolution

If a class uses multiple traits that define methods with the same name, PHP throws a fatal error. Because PHP does not support method overloading by signature, any matching method name causes a collision regardless of its parameters or return type. You must explicitly resolve these naming collisions using the insteadof and as operators within a block appended to the use statement.
  • insteadof: Instructs the compiler to use a specific trait’s method over another.
  • as: Creates an alias for a method, allowing the overridden method to still be accessed under a different name.
trait TraitA {
    public function execute(): void { echo "A"; }
}

trait TraitB {
    public function execute(): void { echo "B"; }
}

class Task {
    use TraitA, TraitB {
        TraitA::execute insteadof TraitB;
        TraitB::execute as executeB;
    }
}

Property and Constant Conflicts

Unlike methods, conflicts involving properties or constants (supported in traits as of PHP 8.2) cannot be resolved using the insteadof or as operators. If multiple traits define a property with the same name, or if the composing class defines a property with the same name as a trait, PHP throws a fatal error. The collision is only permitted if the properties are strictly compatible. To be strictly compatible, the properties must share the exact same:
  • Visibility (public, protected, private)
  • Type declaration
  • readonly modifier
  • Initial value
trait StateA {
    public string $status = 'active';
    public int $code = 1;
}

trait StateB {
    public string $status = 'active'; // Strictly compatible: Allowed
    // public int $code = 2;          // Incompatible initial value: Fatal Error
}

class Entity {
    use StateA, StateB;
    
    public string $status = 'active'; // Strictly compatible: Allowed
}

Visibility Modification

The as operator can mutate the access modifier (public, protected, private) of a trait method within the composing class. This can be done with or without aliasing the method name.
trait Utility {
    private function calculate(): int {
        return 42;
    }
}

class Service {
    use Utility {
        // Elevate visibility to public without renaming
        calculate as public;
        
        // Elevate visibility to public AND alias the method
        calculate as public getCalculation;
    }
}

Trait Composition

Traits can utilize the use statement to compose other traits. The syntax and conflict resolution mechanics remain identical to those used within classes.
trait Database {
    public function connect(): void {}
}

trait Cache {
    public function set(): void {}
}

trait Repository {
    use Database, Cache;
    
    public function save(): void {
        $this->connect();
        $this->set();
    }
}
Master PHP with Deep Grasping Methodology!Learn More