Challenge - 5 Problems
PHP Use Keyword Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of class usage with alias
What is the output of this PHP code when using the
use keyword with an alias?PHP
<?php namespace Animals\Mammals; class Dog { public function speak() { return "Woof!"; } } namespace Pets; use Animals\Mammals\Dog as PetDog; $dog = new PetDog(); echo $dog->speak();
Attempts:
2 left
💡 Hint
Remember that the
use keyword imports the class with the alias, so you can create an instance using the alias name.✗ Incorrect
The
use Animals\Mammals\Dog as PetDog; imports the Dog class under the alias PetDog. Creating new PetDog() calls the Dog class's speak method, which returns 'Woof!'.❓ Predict Output
intermediateOutput when importing multiple classes
What will be the output of this PHP code that imports multiple classes using the
use keyword?PHP
<?php namespace Vehicles\Land; class Car { public function type() { return "Car"; } } class Bike { public function type() { return "Bike"; } } namespace Garage; use Vehicles\Land\Car; use Vehicles\Land\Bike; $car = new Car(); $bike = new Bike(); echo $car->type() . " and " . $bike->type();
Attempts:
2 left
💡 Hint
Each class is imported separately with its full namespace path.
✗ Incorrect
Both classes Car and Bike are imported correctly. Creating instances and calling their type methods returns 'Car' and 'Bike', concatenated with ' and '.
❓ Predict Output
advancedOutput when importing functions with use keyword
What is the output of this PHP code that imports a function using the
use function syntax?PHP
<?php namespace Math\Operations; function add($a, $b) { return $a + $b; } namespace Calculator; use function Math\Operations\add; echo add(5, 7);
Attempts:
2 left
💡 Hint
The
use function imports a function from another namespace so it can be called directly.✗ Incorrect
The function add is imported from Math\Operations namespace. Calling add(5, 7) returns 12.
❓ Predict Output
advancedOutput when importing constants with use keyword
What will this PHP code output when importing a constant using the
use const keyword?PHP
<?php namespace Config; const VERSION = '1.2.3'; namespace App; use const Config\VERSION; echo "App version: " . VERSION;
Attempts:
2 left
💡 Hint
Constants can be imported with
use const to be used without namespace prefix.✗ Incorrect
The constant VERSION is imported from Config namespace and used directly, printing 'App version: 1.2.3'.
❓ Predict Output
expertOutput when importing multiple elements with group use syntax
What is the output of this PHP code that uses group use syntax to import multiple classes and functions?
PHP
<?php namespace Library\Utils { class Formatter { public static function format() { return "Formatted"; } } function helper() { return "Helped"; } } namespace App; use Library\Utils\{Formatter, function helper}; echo Formatter::format() . " and " . helper();
Attempts:
2 left
💡 Hint
Group use syntax imports multiple elements from the same namespace in one statement.
✗ Incorrect
The group use imports Formatter class and helper function. Calling Formatter::format() returns 'Formatted' and helper() returns 'Helped'.
