Bird
Raised Fist0
PHPprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of the use keyword in PHP?
easy
A. To import classes or namespaces into the current file
B. To declare a new function
C. To start a PHP script
D. To create a new variable

Solution

  1. Step 1: Understand the role of use

    The use keyword is used to import classes or namespaces so you can use them without full names.
  2. Step 2: Compare with other options

    Declaring functions, starting scripts, or creating variables are unrelated to use.
  3. Final Answer:

    To import classes or namespaces into the current file -> Option A
  4. Quick Check:

    Importing code = use keyword [OK]
Hint: Remember: use imports code to simplify names [OK]
Common Mistakes:
  • Thinking use declares functions
  • Confusing use with variable assignment
  • Using use inside functions instead of top of file
2. Which of the following is the correct syntax to import the class Database\Connection using the use keyword?
easy
A. require Database\Connection;
B. import Database\Connection;
C. use Database\Connection;
D. include Database\Connection;

Solution

  1. Step 1: Identify correct import syntax

    In PHP, use is the correct keyword to import namespaces or classes.
  2. Step 2: Eliminate other keywords

    import is not valid PHP syntax; include and require load files but do not import namespaces.
  3. Final Answer:

    use Database\Connection; -> Option C
  4. Quick Check:

    use keyword imports namespaces [OK]
Hint: Use 'use' keyword, not import/include/require for namespaces [OK]
Common Mistakes:
  • Using import instead of use
  • Confusing include with use
  • Missing semicolon after use statement
3. Given the code:
namespace App;

use Library\Tools as ToolsAlias;

$tool = new ToolsAlias();
echo get_class($tool);

What will be the output?
medium
A. ToolsAlias
B. Library\Tools
C. App\ToolsAlias
D. Error: class not found

Solution

  1. Step 1: Understand aliasing with as

    The use statement imports Library\Tools as ToolsAlias, so new ToolsAlias() creates an instance of Library\Tools.
  2. Step 2: Check output of get_class()

    The get_class() function returns the full class name, which is Library\Tools.
  3. Final Answer:

    Library\Tools -> Option B
  4. Quick Check:

    Alias points to original class name [OK]
Hint: Alias keeps original class name for get_class() [OK]
Common Mistakes:
  • Expecting alias name as output
  • Confusing namespace with alias
  • Assuming error without class definition
4. What is wrong with this PHP code?
<?php
use MyApp\Utils;

function test() {
    use MyApp\Helpers;
}
medium
A. You cannot use use inside a function
B. Missing semicolon after use statement
C. Namespace name is invalid
D. Functions cannot be declared after use

Solution

  1. Step 1: Check placement of use keyword

    The use keyword for importing namespaces must be at the top level, not inside functions.
  2. Step 2: Validate other syntax

    Semicolons are present; namespace names look valid; functions can be declared after use statements.
  3. Final Answer:

    You cannot use use inside a function -> Option A
  4. Quick Check:

    use statements only at file top [OK]
Hint: Place use statements only at top, not inside functions [OK]
Common Mistakes:
  • Trying to import inside functions
  • Forgetting semicolons
  • Misunderstanding namespace rules
5. You have two classes with the same name Logger in different namespaces: Framework\Logger and App\Logger. How can you import both in one PHP file to use them without full names?
hard
A. use Framework\Logger; use App\Logger;
B. Cannot import two classes with same name
C. use Framework\Logger, App\Logger;
D. use Framework\Logger as FrameworkLogger; use App\Logger as AppLogger;

Solution

  1. Step 1: Understand name conflicts with imports

    When two classes share the same name, you must alias at least one to avoid conflicts.
  2. Step 2: Check options for aliasing

    use Framework\Logger as FrameworkLogger; use App\Logger as AppLogger; uses as to rename both classes uniquely. use Framework\Logger; use App\Logger; imports both without alias, causing conflict. use Framework\Logger, App\Logger; syntax is invalid. Cannot import two classes with same name is false.
  3. Final Answer:

    use Framework\Logger as FrameworkLogger; use App\Logger as AppLogger; -> Option D
  4. Quick Check:

    Alias same names to avoid conflicts [OK]
Hint: Use 'as' to rename same-named imports [OK]
Common Mistakes:
  • Importing same class names without alias
  • Using comma to import multiple classes in one use
  • Thinking it's impossible to import both
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard