0
0
PHPprogramming~5 mins

Aliasing with as keyword in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the as keyword do in PHP when used with namespaces?
The as keyword lets you give a different name (alias) to a class, function, or namespace when importing it. This helps avoid name conflicts or makes names shorter and easier to use.
Click to reveal answer
beginner
How do you alias a class named <code>Library\Book</code> to <code>Novel</code> in PHP?
You write: <br>use Library\Book as Novel;<br>This means you can use Novel in your code instead of the full Library\Book name.
Click to reveal answer
beginner
Why is aliasing useful in PHP namespaces?
Aliasing helps when two classes or functions have the same name but come from different places. It avoids confusion and errors by letting you rename one or both when you import them.
Click to reveal answer
intermediate
Show an example of aliasing two classes with the same name from different namespaces.
Example:<br>use Library\Book as LibraryBook;<br>use Store\Book as StoreBook;<br>Now you can use LibraryBook and StoreBook to tell them apart.
Click to reveal answer
intermediate
Can you alias functions or constants using the as keyword in PHP?
Yes, PHP 5.6+ allows aliasing functions and constants imported from namespaces using as. This works similarly to classes to avoid name conflicts.
Click to reveal answer
What is the purpose of the as keyword in PHP namespaces?
ATo create an alias for a class, function, or namespace
BTo declare a new namespace
CTo import all classes from a namespace
DTo define a constant
How would you alias App\Controller\User as UserController?
Aalias App\Controller\User UserController;
Buse App\Controller\User as UserController;
Cimport App\Controller\User as UserController;
Dnamespace App\Controller\User as UserController;
If two classes have the same name but different namespaces, how can aliasing help?
AIt disables one namespace
BIt merges the two classes automatically
CIt deletes one of the classes
DIt lets you rename one or both to avoid conflicts
Which PHP version introduced aliasing for functions and constants?
APHP 5.6
BPHP 7.0
CPHP 5.3
DPHP 8.0
What happens if you don't alias two classes with the same name from different namespaces?
ABoth classes merge into one
BPHP automatically renames one
CYou get a fatal error due to name conflict
DThe second class is ignored
Explain how the as keyword helps when using namespaces in PHP.
Think about how you can rename something imported from another place.
You got /4 concepts.
    Write a PHP example showing how to alias two classes with the same name from different namespaces.
    Use <code>use Namespace\Class as Alias;</code> twice with different namespaces.
    You got /4 concepts.