Challenge - 5 Problems
PHP Aliasing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of aliased class usage
What is the output of this PHP code using aliasing with the
as keyword?PHP
<?php namespace Animals; class Dog { public function speak() { return "Woof!"; } } namespace Pets; use Animals\Dog as PetDog; $dog = new PetDog(); echo $dog->speak(); ?>
Attempts:
2 left
💡 Hint
Look at how the
use statement renames the class from another namespace.✗ Incorrect
The
use Animals\Dog as PetDog; statement creates an alias PetDog for Animals\Dog. So, new PetDog() creates an instance of Animals\Dog. Calling speak() returns 'Woof!'.❓ Predict Output
intermediate2:00remaining
Aliasing function import output
What will this PHP code output when using aliasing with the
as keyword for functions?PHP
<?php namespace Utils; function greet() { return "Hello!"; } namespace Main; use function Utils\greet as sayHello; echo sayHello(); ?>
Attempts:
2 left
💡 Hint
Check how the function is imported with an alias.
✗ Incorrect
The
use function Utils\greet as sayHello; imports the function greet from the Utils namespace and aliases it as sayHello. Calling sayHello() runs Utils\greet() and returns 'Hello!'.🔧 Debug
advanced2:00remaining
Identify the error in aliasing a constant
This PHP code tries to alias a constant using the
as keyword. What error will it produce?PHP
<?php namespace Config; const VERSION = '1.0'; namespace App; use const Config\VERSION as APP_VERSION; echo APP_VERSION; ?>
Attempts:
2 left
💡 Hint
PHP supports aliasing constants with
use const and as.✗ Incorrect
The code correctly aliases the constant
VERSION from Config namespace as APP_VERSION. Echoing APP_VERSION outputs '1.0'. No error occurs.📝 Syntax
advanced2:00remaining
Which aliasing syntax is invalid?
Which of the following PHP aliasing statements using
as keyword is NOT valid syntax?Attempts:
2 left
💡 Hint
Check the correct keyword order and syntax for aliasing.
✗ Incorrect
Option B is invalid because the correct syntax uses the
as keyword, not the word 'alias'. The correct form is use Some\Namespace\ClassName as ClassAlias;.🚀 Application
expert3:00remaining
Predict the output with multiple aliasing
Given this PHP code with multiple aliasing, what will be the output?
PHP
<?php namespace A { class Test { public function msg() { return "From A"; } } } namespace B { class Test { public function msg() { return "From B"; } } } namespace Main { use A\Test as TestA; use B\Test as TestB; $a = new TestA(); $b = new TestB(); echo $a->msg() . ' & ' . $b->msg(); } ?>
Attempts:
2 left
💡 Hint
Aliasing allows using two classes with the same name from different namespaces.
✗ Incorrect
The code aliases
A\Test as TestA and B\Test as TestB. Creating objects and calling msg() returns 'From A' and 'From B' respectively, joined by ' & '.