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?✗ Incorrect
The
as keyword creates an alias to rename imported classes, functions, or namespaces.How would you alias
App\Controller\User as UserController?✗ Incorrect
The correct syntax to alias is using
use ... as ...;.If two classes have the same name but different namespaces, how can aliasing help?
✗ Incorrect
Aliasing renames classes to avoid name conflicts when both are used.
Which PHP version introduced aliasing for functions and constants?
✗ Incorrect
PHP 5.6 added support for aliasing functions and constants.
What happens if you don't alias two classes with the same name from different namespaces?
✗ Incorrect
Without aliasing, PHP throws an error because it can't distinguish the classes.
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.