0
0
PHPprogramming~5 mins

Sub-namespaces in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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'?
Anamespace App\\Models;
Bnamespace App.Models;
Cnamespace App/Models;
Dnamespace App-Models;
What symbol separates sub-namespaces in PHP?
A\\
B/
C.
D-
If you are in namespace 'App\\Controllers', how do you create an object from 'App\\Models\\User'?
Anew Models\\User();
Bnew User();
Cnew \\App\\Models\\User();
Dnew App\\Models\\User();
Why are sub-namespaces useful?
AThey make code run faster.
BThey replace classes.
CThey reduce file size.
DThey organize code and avoid name conflicts.
What happens if you forget the leading backslash when accessing a class in a different namespace?
APHP automatically finds the class.
BPHP throws an error or looks in the wrong namespace.
CThe class is loaded from global namespace.
DThe code runs slower.
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.