The use keyword imports classes, functions, or namespaces so you can use them without writing the full path every time.
Click to reveal answer
beginner
How do you import a class named <code>Car</code> from the namespace <code>Vehicles</code>?
You write: use Vehicles\Car; This lets you use Car directly in your code.
Click to reveal answer
intermediate
Can you import multiple classes from the same namespace using <code>use</code>?
Yes. You can import multiple classes by listing them separated by commas:<br><code>use Vehicles\{Car, Bike, Truck};</code>
Click to reveal answer
intermediate
What is aliasing in PHP use statements?
Aliasing lets you rename an imported class or namespace to a shorter or different name using <code>as</code>.<br>Example:<br><code>use Vehicles\Car as Auto;</code>
Click to reveal answer
beginner
Why is importing with use helpful in PHP?
It makes code cleaner and easier to read by avoiding long full paths. It also helps avoid name conflicts by using aliases.
Click to reveal answer
What does the PHP use keyword do?
ADeclares a new class
BImports classes or namespaces for easier use
CStarts a PHP script
DDefines a function
✗ Incorrect
The use keyword imports classes or namespaces so you can use them without full paths.
How do you import multiple classes from the same namespace?
Ause Namespace\{Class1, Class2};
Bimport Namespace\Class1, Class2;
Cuse Namespace\Class1, Namespace\Class2;
Dinclude Namespace\Class1, Class2;
✗ Incorrect
You use curly braces to import multiple classes from the same namespace: use Namespace\{Class1, Class2};
What is aliasing in PHP use statements?
ADeleting an imported class
BCreating a new namespace
CRenaming an imported class or namespace
DCalling a function
✗ Incorrect
Aliasing lets you rename an imported class or namespace using as.
Which of these is a correct aliasing example?
Ause Vehicles\Car alias Auto;
Buse Vehicles\Car to Auto;
Cimport Vehicles\Car as Auto;
Duse Vehicles\Car as Auto;
✗ Incorrect
The correct syntax for aliasing is use Namespace\Class as Alias;
Why should you use the use keyword?
ATo make code cleaner and avoid long names
BTo run PHP scripts faster
CTo declare variables
DTo comment code
✗ Incorrect
Using use makes code cleaner and easier to read by avoiding long full paths.
Explain how the use keyword helps when working with namespaces in PHP.
Think about how you avoid writing long names repeatedly.
You got /4 concepts.
Describe how to import multiple classes from the same namespace and how to rename one using aliasing.
Remember the special syntax with braces and the keyword 'as'.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the use keyword in PHP?
easy
A. To import classes or namespaces into the current file
B. To declare a new function
C. To start a PHP script
D. To create a new variable
Solution
Step 1: Understand the role of use
The use keyword 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 to use.
Final Answer:
To import classes or namespaces into the current file -> Option A
Quick Check:
Importing code = use keyword [OK]
Hint: Remember: use imports code to simplify names [OK]
Common Mistakes:
Thinking use declares functions
Confusing use with variable assignment
Using use inside functions instead of top of file
2. Which of the following is the correct syntax to import the class Database\Connection using the use keyword?
easy
A. require Database\Connection;
B. import Database\Connection;
C. use Database\Connection;
D. include Database\Connection;
Solution
Step 1: Identify correct import syntax
In PHP, use is the correct keyword to import namespaces or classes.
Step 2: Eliminate other keywords
import is not valid PHP syntax; include and require load files but do not import namespaces.
Final Answer:
use Database\Connection; -> Option C
Quick Check:
use keyword imports namespaces [OK]
Hint: Use 'use' keyword, not import/include/require for namespaces [OK]
Common Mistakes:
Using import instead of use
Confusing include with use
Missing semicolon after use statement
3. Given the code:
namespace App;
use Library\Tools as ToolsAlias;
$tool = new ToolsAlias();
echo get_class($tool);
What will be the output?
medium
A. ToolsAlias
B. Library\Tools
C. App\ToolsAlias
D. Error: class not found
Solution
Step 1: Understand aliasing with as
The use statement imports Library\Tools as ToolsAlias, so new ToolsAlias() creates an instance of Library\Tools.
Step 2: Check output of get_class()
The get_class() function returns the full class name, which is Library\Tools.
Final Answer:
Library\Tools -> Option B
Quick Check:
Alias points to original class name [OK]
Hint: Alias keeps original class name for get_class() [OK]
Common Mistakes:
Expecting alias name as output
Confusing namespace with alias
Assuming error without class definition
4. What is wrong with this PHP code?
<?php
use MyApp\Utils;
function test() {
use MyApp\Helpers;
}
medium
A. You cannot use use inside a function
B. Missing semicolon after use statement
C. Namespace name is invalid
D. Functions cannot be declared after use
Solution
Step 1: Check placement of use keyword
The use keyword 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 use use inside a function -> Option A
Quick Check:
use statements only at file top [OK]
Hint: Place use statements only at top, not inside functions [OK]
Common Mistakes:
Trying to import inside functions
Forgetting semicolons
Misunderstanding namespace rules
5. You have two classes with the same name Logger in different namespaces: Framework\Logger and App\Logger. How can you import both in one PHP file to use them without full names?
hard
A. use Framework\Logger;
use App\Logger;
B. Cannot import two classes with same name
C. use Framework\Logger, App\Logger;
D. use Framework\Logger as FrameworkLogger;
use App\Logger as AppLogger;
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; uses as to 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 D
Quick Check:
Alias same names to avoid conflicts [OK]
Hint: Use 'as' to rename same-named imports [OK]
Common Mistakes:
Importing same class names without alias
Using comma to import multiple classes in one use
Thinking it's impossible to import both
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.