0
0
PHPprogramming~20 mins

Importing with use keyword in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PHP Use Keyword Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output 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();
ANothing is printed
BFatal error: Class 'Pets\Dog' not found
CWoof!
DWoof Woof!
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.
Predict Output
intermediate
2:00remaining
Output 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();
ACar and Car
BCar and Bike
CCarBike
DFatal error: Class 'Garage\Bike' not found
Attempts:
2 left
💡 Hint
Each class is imported separately with its full namespace path.
Predict Output
advanced
2:00remaining
Output 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);
A12
BFatal error: Call to undefined function add()
C57
D5 + 7
Attempts:
2 left
💡 Hint
The use function imports a function from another namespace so it can be called directly.
Predict Output
advanced
2:00remaining
Output 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;
AApp version: 123
BApp version: VERSION
CFatal error: Undefined constant 'VERSION'
DApp version: 1.2.3
Attempts:
2 left
💡 Hint
Constants can be imported with use const to be used without namespace prefix.
Predict Output
expert
2:00remaining
Output 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();
AFormatted and Helped
BFatal error: Class 'App\Formatter' not found
CFormatted and helper
DHelped and Formatted
Attempts:
2 left
💡 Hint
Group use syntax imports multiple elements from the same namespace in one statement.