0
0
PHPprogramming~10 mins

Sub-namespaces in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Sub-namespaces
Declare main namespace
Declare sub-namespace
Define class/function in sub-namespace
Use fully qualified name or import
Access class/function in sub-namespace
This flow shows how PHP code declares a main namespace, then a sub-namespace inside it, defines code there, and accesses it using full or imported names.
Execution Sample
PHP
<?php
namespace Main\Sub;
class Test {
  public function say() { return "Hello from Sub!"; }
}
$obj = new \Main\Sub\Test();
echo $obj->say();
Defines a class inside a sub-namespace and creates an object using the full namespace path, then calls a method.
Execution Table
StepCode LineActionNamespace ContextResult/Output
1namespace Main\Sub;Declare sub-namespace inside MainMain\SubNamespace set to Main\Sub
2class Test {...}Define class Test inside Main\SubMain\SubClass Test available in Main\Sub
3$obj = new \Main\Sub\Test();Create object using full namespaceGlobalObject of Main\Sub\Test created
4echo $obj->say();Call say() method on objectGlobalOutputs: Hello from Sub!
💡 Code finishes after outputting the string from the sub-namespace class method.
Variable Tracker
VariableStartAfter Step 3After Step 4
$objundefinedInstance of Main\Sub\TestInstance of Main\Sub\Test
Key Moments - 2 Insights
Why do we use a backslash before Main\Sub when creating the object?
The backslash means we use the full global namespace path. Without it, PHP looks inside the current namespace, which might cause errors. See step 3 in execution_table.
Can we define multiple sub-namespaces inside one file?
Yes, but each namespace block must be declared separately. Here, we only use one sub-namespace 'Main\Sub' as shown in step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the namespace context when the class Test is defined?
AMain\Sub
BMain
CGlobal
DUndefined
💡 Hint
Check step 2 in the execution_table where the class is defined.
At which step is the object $obj created?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for the line with 'new \Main\Sub\Test()' in the execution_table.
If we remove the leading backslash in 'new \Main\Sub\Test()', what happens?
AObject is created successfully
BPHP looks in current namespace and may cause error
CNamespace changes to global automatically
DNo effect, same output
💡 Hint
Refer to key_moments question about the backslash usage.
Concept Snapshot
PHP sub-namespaces are declared with namespace Main\Sub; inside a file.
Classes/functions inside belong to that sub-namespace.
Use leading backslash for full path: new \Main\Sub\ClassName();
Without backslash, PHP looks inside current namespace.
Sub-namespaces help organize code hierarchically.
Full Transcript
This example shows how PHP uses sub-namespaces to organize code. First, the code declares namespace Main\Sub, which means everything inside belongs to that sub-namespace. Then, a class Test is defined inside it. When creating an object, the code uses the full path with a leading backslash: new \Main\Sub\Test(). This tells PHP to look from the global namespace root. Calling the say() method on the object outputs a string. The execution table traces each step, showing namespace context and results. The variable tracker shows how $obj changes from undefined to an instance. Key moments explain why the backslash is important and how namespaces work. The quiz tests understanding of namespace context, object creation step, and effects of removing the backslash. The snapshot summarizes the main points about sub-namespaces in PHP.