Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a namespace named MyApp.
PHP
<?php namespace [1]; class User {}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using reserved words as namespace names
Omitting the namespace keyword
✗ Incorrect
The namespace keyword is followed by the namespace name, here MyApp.
2fill in blank
mediumComplete the code to use the class User from the namespace MyApp.
PHP
<?php
use [1]\User;
$user = new User();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong namespace name
Forgetting to import the class
✗ Incorrect
The use statement imports the class User from the namespace MyApp.
3fill in blank
hardFix the error in the code by completing the blank to refer to the global class Exception.
PHP
<?php try { throw new \[1]("Error occurred"); } catch (\Exception $e) { echo $e->getMessage(); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the leading backslash
Using wrong class names
✗ Incorrect
The global Exception class must be referenced with a leading backslash to avoid namespace conflicts.
4fill in blank
hardFill both blanks to create a namespace and define a class inside it.
PHP
<?php namespace [1]; class [2] {}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing namespace and class names
Using invalid identifiers
✗ Incorrect
The namespace is App and the class defined inside is Product.
5fill in blank
hardFill all three blanks to import a class and create its instance.
PHP
<?php use [1]\[2]; $obj = new [3]();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong namespace or class names
Forgetting to import before instantiation
✗ Incorrect
The class User from namespace MyApp is imported and instantiated.