> ## 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 Use Statement

The `use` statement in PHP serves three distinct, language-level purposes: importing and aliasing constructs (classes, interfaces, traits, functions, and constants) within namespaces, binding variables from the lexical parent scope into the local scope of an anonymous function (closure), and including traits within class declarations.

## Namespace Importing and Aliasing

In the context of namespaces, the `use` keyword instructs the PHP compiler to alias a Fully Qualified Name (FQN) to an unqualified or qualified name. This resolution occurs strictly at compile-time, meaning it does not trigger autoloading until the imported construct is actually instantiated or invoked.

By default, importing a FQN implicitly aliases it to its un-namespaced base name. The `as` operator allows for explicit aliasing to resolve naming collisions.

```php theme={"dark"}
// Implicitly aliases to the base name 'Database'
use Core\Database\Connections\Database;

// Explicitly aliases to 'DbConnection'
use Core\Database\Connections\Database as DbConnection;
```

### Importing Functions and Constants

While classes, interfaces, and traits are imported using the standard `use` statement, importing functions and constants requires the `use function` and `use const` modifiers, respectively.

```php theme={"dark"}
use function StringHelpers\capitalize;
use function MathHelpers\calculate_tax as tax;

use const Configuration\MAX_EXECUTION_TIME;
```

### Grouped Declarations

PHP 7.0 introduced grouped `use` declarations, allowing multiple constructs from the same parent namespace to be imported within a single statement using curly braces `{}`.

```php theme={"dark"}
// Grouping classes
use App\Http\Controllers\{UserController, PostController, AuthController};

// Grouping with mixed aliases
use App\Services\{
    PaymentService as Payment,
    EmailService as Email
};

// Grouping mixed types (PHP 7.0+)
use App\Core\{
    Logger,
    function initialize_app,
    const APP_VERSION
};
```

## Closure Variable Binding

When defining an anonymous function, the `use` construct manually inherits variables from the defining (parent) scope into the closure's execution scope. This is necessary because PHP does not automatically inherit the lexical scope.

Variables bound via `use` are evaluated and copied at the time the closure is defined, not when it is executed. By default, variables are passed by value. To modify the original variable within the parent scope, it must be passed by reference using the `&` operator.

```php theme={"dark"}
$threshold = 100;
$executionCount = 0;

// $threshold is bound by value
// $executionCount is bound by reference
$processor = function(int $value) use ($threshold, &$executionCount): bool {
    $executionCount++;
    return $value > $threshold;
};
```

*Note: Arrow functions (`fn() => ...`) introduced in PHP 7.4 automatically capture variables from the parent scope by value and do not support the `use` clause at all. If pass-by-reference binding is required, developers must fall back to a standard anonymous function.*

## Trait Inclusion and Conflict Resolution

Inside a class declaration, the `use` keyword imports methods and properties from one or more traits into the class's scope, providing horizontal code reuse.

When multiple traits introduce method naming collisions, the `use` statement block utilizes the `insteadof` operator to dictate method precedence. Additionally, the `as` operator can be applied within this block to alias methods or alter their visibility modifiers.

```php theme={"dark"}
trait Logger {
    public function log(string $msg): void { /* ... */ }
}

trait FileLogger {
    public function log(string $msg): void { /* ... */ }
    public function setup(): void { /* ... */ }
}

class Application {
    // Importing multiple traits
    use Logger, FileLogger {
        // Conflict resolution: use FileLogger's log method
        FileLogger::log insteadof Logger;
        
        // Aliasing Logger's log method to a new name
        Logger::log as baseLog;
        
        // Altering visibility of FileLogger's setup method
        FileLogger::setup as protected;
    }
}
```

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