Global namespace access lets you use functions, classes, or constants defined outside any namespace. It helps avoid confusion when you have code inside namespaces but want to use global code.
Global namespace access in PHP
<?php namespace MyNamespace; // Access global function \strlen('text'); // Access global class $obj = new \DateTime(); // Access global constant $value = \PHP_VERSION; ?>
The backslash \ before a name tells PHP to look in the global namespace.
Without the backslash, PHP looks inside the current namespace.
strlen function even though the code is inside the MyApp namespace.<?php namespace MyApp; // Calls global strlen function $length = \strlen('hello'); echo $length;
DateTime object from the global namespace inside a namespaced file.<?php namespace MyApp; // Create global DateTime object $date = new \DateTime(); echo $date->format('Y-m-d');
<?php namespace MyApp; // Use global constant echo \PHP_VERSION;
This program shows the difference between calling a function inside the namespace and calling the global function using the backslash. It also creates a global DateTime object and prints the PHP version.
<?php namespace Example; function strlen($str) { return "Namespaced strlen called"; } // Call namespaced strlen echo strlen('test') . "\n"; // Call global strlen echo \strlen('test') . "\n"; // Create global DateTime object $date = new \DateTime(); echo $date->format('Y-m-d') . "\n"; // Print global constant echo \PHP_VERSION . "\n";
Always use the leading backslash \ to access global code inside namespaces.
If you forget the backslash, PHP will look inside the current namespace and may cause errors or unexpected results.
Global namespace access uses a leading backslash \ to call global functions, classes, or constants inside namespaces.
This helps avoid naming conflicts and ensures you use the intended global code.
Practice
\ do when used before a function name inside a PHP namespace?Solution
Step 1: Understand namespaces in PHP
Namespaces group code to avoid name conflicts but can hide global functions.Step 2: Role of leading backslash
The leading backslash tells PHP to look for the function in the global namespace, ignoring the current namespace.Final Answer:
It accesses the function from the global namespace. -> Option DQuick Check:
Leading backslash = global access [OK]
- Thinking \ creates a new function
- Assuming \ calls current namespace function
- Confusing \ with escape character
strlen function inside a namespace?Solution
Step 1: Identify global function call syntax
To call a global function inside a namespace, prefix it with a backslash \.Step 2: Check each option
\strlen('test'); uses \strlen('test'); which correctly calls the global strlen function.Final Answer:
\strlen('test'); -> Option AQuick Check:
Backslash before function = global call [OK]
- Omitting the leading backslash inside namespaces
- Using 'global' keyword incorrectly
- Trying to prefix with 'namespace' keyword
namespace MyApp;
function strlen($str) {
return 42;
}
echo strlen('hello');
echo "-";
echo \strlen('hello');Solution
Step 1: Understand function calls inside namespace
Calling strlen('hello') calls MyApp\strlen which returns 42.Step 2: Understand global function call
Calling \strlen('hello') calls global strlen which returns length 5.Final Answer:
42-5 -> Option AQuick Check:
Namespaced strlen = 42, global strlen = 5 [OK]
- Assuming both calls return same value
- Mixing up which strlen is called
- Ignoring namespace effect on function calls
namespace Test;
function array_sum($arr) {
return 0;
}
$result = array_sum([1, 2, 3]);
$result2 = \array_sum([1, 2, 3]);
echo $result . ',' . $result2;Solution
Step 1: Check function definitions and calls
array_sum is redefined in namespace Test to always return 0.Step 2: Understand calls
array_sum([1,2,3]) calls namespaced function returning 0; \array_sum calls global function returning 6.Final Answer:
No error, code runs and outputs '0,6'. -> Option CQuick Check:
Namespaced and global functions both called correctly [OK]
- Thinking \array_sum is undefined
- Expecting syntax errors from backslash
- Confusing namespace keyword usage
MyApp\Logger and a global class Logger. How do you instantiate the global Logger inside the MyApp namespace?Solution
Step 1: Understand class instantiation in namespaces
Without backslash, new Logger() looks for MyApp\Logger inside the namespace.Step 2: Use leading backslash to access global class
new \Logger() creates an instance of the global Logger class.Final Answer:
$log = new \Logger(); -> Option BQuick Check:
Leading \ before class = global class instantiation [OK]
- Omitting \ and getting namespaced class
- Using 'global\' which is invalid
- Confusing namespace prefix with global access
Ready to prove it?
Pass the test and this pattern moves unknown
