The use keyword helps you bring code from other places into your file so you can use it easily without writing full names every time.
Importing with use keyword in PHP
<?php use Namespace\SubNamespace\ClassName; // or with alias use Namespace\SubNamespace\ClassName as AliasName;
The use statement must be placed at the top of your PHP file, after the <?php tag and before any other code.
You can import multiple classes or functions by writing multiple use lines.
User class from the App\Models namespace so you can create a new user easily.<?php use App\Models\User; $user = new User();
Formatter class but renames it to Fmt to keep the code shorter.<?php use App\Helpers\Formatter as Fmt; echo Fmt::formatDate('2024-06-01');
calculateSum from a namespace so you can call it directly.<?php use function App\Utils\calculateSum; $result = calculateSum(5, 10);
This program defines a User class in one namespace and then imports it in another namespace using use. It creates a User object and calls its greet method.
<?php namespace App\Models; class User { public function greet() { return "Hello from User!"; } } namespace App\Main; use App\Models\User; $user = new User(); echo $user->greet();
If you forget to import a class with use, PHP will look for it in the current namespace and may give an error if not found.
You can import classes, functions, and constants using use with slightly different syntax.
Using aliases with as helps when two imported classes have the same name.
The use keyword imports code from other namespaces or files to make your code cleaner.
Place use statements at the top of your PHP file after <?php.
You can rename imports with as to avoid name conflicts or shorten names.
Practice
use keyword in PHP?Solution
Step 1: Understand the role of
Theuseusekeyword is used to import classes or namespaces so you can use them without full names.Step 2: Compare with other options
Declaring functions, starting scripts, or creating variables are unrelated touse.Final Answer:
To import classes or namespaces into the current file -> Option AQuick Check:
Importing code = use keyword [OK]
- Thinking use declares functions
- Confusing use with variable assignment
- Using use inside functions instead of top of file
Database\Connection using the use keyword?Solution
Step 1: Identify correct import syntax
In PHP,useis the correct keyword to import namespaces or classes.Step 2: Eliminate other keywords
importis not valid PHP syntax;includeandrequireload files but do not import namespaces.Final Answer:
use Database\Connection; -> Option CQuick Check:
use keyword imports namespaces [OK]
- Using import instead of use
- Confusing include with use
- Missing semicolon after use statement
namespace App; use Library\Tools as ToolsAlias; $tool = new ToolsAlias(); echo get_class($tool);
What will be the output?
Solution
Step 1: Understand aliasing with
Theasusestatement importsLibrary\ToolsasToolsAlias, sonew ToolsAlias()creates an instance ofLibrary\Tools.Step 2: Check output of
Theget_class()get_class()function returns the full class name, which isLibrary\Tools.Final Answer:
Library\Tools -> Option BQuick Check:
Alias points to original class name [OK]
- Expecting alias name as output
- Confusing namespace with alias
- Assuming error without class definition
<?php
use MyApp\Utils;
function test() {
use MyApp\Helpers;
}Solution
Step 1: Check placement of
Theusekeywordusekeyword for importing namespaces must be at the top level, not inside functions.Step 2: Validate other syntax
Semicolons are present; namespace names look valid; functions can be declared after use statements.Final Answer:
You cannot useuseinside a function -> Option AQuick Check:
use statements only at file top [OK]
- Trying to import inside functions
- Forgetting semicolons
- Misunderstanding namespace rules
Logger in different namespaces: Framework\Logger and App\Logger. How can you import both in one PHP file to use them without full names?Solution
Step 1: Understand name conflicts with imports
When two classes share the same name, you must alias at least one to avoid conflicts.Step 2: Check options for aliasing
use Framework\Logger as FrameworkLogger; use App\Logger as AppLogger; usesasto rename both classes uniquely. use Framework\Logger; use App\Logger; imports both without alias, causing conflict. use Framework\Logger, App\Logger; syntax is invalid. Cannot import two classes with same name is false.Final Answer:
use Framework\Logger as FrameworkLogger; use App\Logger as AppLogger; -> Option DQuick Check:
Alias same names to avoid conflicts [OK]
- Importing same class names without alias
- Using comma to import multiple classes in one use
- Thinking it's impossible to import both
Ready to prove it?
Pass the test and this pattern moves unknown
