Recall & Review
beginner
What is a sub-namespace in PHP?
A sub-namespace is a namespace nested inside another namespace, used to organize code into smaller, related groups.
Click to reveal answer
beginner
How do you declare a sub-namespace in PHP?
Use backslashes to separate namespace levels, for example:
namespace MyApp\\Utils\\Helpers; declares a sub-namespace Helpers inside Utils.Click to reveal answer
beginner
Why use sub-namespaces instead of one big namespace?
Sub-namespaces help keep code organized, avoid name conflicts, and make it easier to find related classes or functions.
Click to reveal answer
beginner
How do you access a class in a sub-namespace?Use the full namespace path with backslashes, for example:
new \\MyApp\\Utils\\Helpers\\MyClass();.Click to reveal answer
intermediate
What happens if you omit the leading backslash when accessing a sub-namespace class?
PHP looks for the class relative to the current namespace, which may cause errors if the full path is not correct.Click to reveal answer
How do you declare a sub-namespace named 'Models' inside 'App'?
✗ Incorrect
In PHP, namespaces use backslashes (\\) to separate levels, so 'namespace App\\Models;' is correct.
What symbol separates sub-namespaces in PHP?
✗ Incorrect
PHP uses the backslash (\\) to separate namespaces and sub-namespaces.
If you are in namespace 'App\\Controllers', how do you create an object from 'App\\Models\\User'?
✗ Incorrect
You need the full path with a leading backslash to access a class outside the current namespace.
Why are sub-namespaces useful?
✗ Incorrect
Sub-namespaces help organize code and prevent name conflicts by grouping related code.
What happens if you forget the leading backslash when accessing a class in a different namespace?
✗ Incorrect
Without the leading backslash, PHP looks relative to the current namespace, which can cause errors.
Explain what sub-namespaces are and why they are useful in PHP.
Think about folders inside folders to keep things tidy.
You got /3 concepts.
Describe how to declare and use a class inside a sub-namespace in PHP.
Remember the backslash \\ is like a folder separator.
You got /3 concepts.