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 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.
// 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.
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 {}.
// 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.
$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.
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;
    }
}
Master PHP with Deep Grasping Methodology!Learn More