> ## 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.

# PHP Trait Use

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.

```php theme={"dark"}
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.

```php theme={"dark"}
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.

```php theme={"dark"}
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

```php theme={"dark"}
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.

```php theme={"dark"}
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.

```php theme={"dark"}
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();
    }
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor PHP Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
