Challenge - 5 Problems
Global Namespace Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of accessing global namespace function
What is the output of this PHP code?
PHP
<?php namespace MyApp; function strlen($str) { return "MyApp strlen: " . strlen($str); } echo strlen("test"); echo "\n"; echo \strlen("test"); echo "\n"; echo \MyApp\strlen("test"); ?>
Attempts:
2 left
💡 Hint
Remember that \strlen calls the global function, while strlen inside namespace calls the local one.
✗ Incorrect
Inside the namespace MyApp, strlen() is redefined. Calling strlen("test") calls the local function, which prefixes output. Calling \strlen("test") calls the global PHP strlen function. Calling \MyApp\strlen("test") calls the namespaced function explicitly.
❓ Predict Output
intermediate2:00remaining
Accessing global constant inside namespace
What will this PHP code output?
PHP
<?php namespace Test; const PI = 3.14; echo PI . "\n"; echo \PI . "\n"; ?>
Attempts:
2 left
💡 Hint
Global constants are not automatically imported into namespaces.
✗ Incorrect
Inside the namespace Test, PI refers to Test\PI which is defined as 3.14. \PI tries to access a global constant PI, which does not exist, causing a fatal error.
🔧 Debug
advanced2:00remaining
Why does this global function call fail?
This code throws an error. What is the cause?
PHP
<?php namespace App; function test() { echo "App test"; } function run() { test(); \test(); } run(); ?>
Attempts:
2 left
💡 Hint
Check if the global namespace has a function named test.
✗ Incorrect
Inside namespace App, test() calls App\test(), which exists. \test() tries to call the global test() function, which does not exist, causing a fatal error.
📝 Syntax
advanced2:00remaining
Which option correctly accesses a global class inside a namespace?
Given a global class Logger, which code correctly creates an instance inside namespace App?
PHP
<?php namespace App; class Logger { public function log() { echo "App Logger"; } } // Global Logger class class \Logger { public function log() { echo "Global Logger"; } } // Which line correctly creates a global Logger instance? // $logger = ???; ?>
Attempts:
2 left
💡 Hint
Use a leading backslash to access global classes from inside a namespace.
✗ Incorrect
Inside namespace App, Logger() refers to App\Logger. To access the global Logger class, use \Logger(). \App\Logger() is valid but redundant inside App namespace. \global\Logger() is invalid syntax.
🚀 Application
expert3:00remaining
How to call a global function named 'process' inside a namespace with a local function of the same name?
You have a global function process() and inside namespace Data you also have a function process(). How do you call the global process() from inside Data namespace?
PHP
<?php function process() { return "global process"; } namespace Data { function process() { return "Data process"; } function run() { // Call global process here return ???; } } echo \Data\run(); ?>
Attempts:
2 left
💡 Hint
Use a leading backslash to call global functions inside namespaces.
✗ Incorrect
Inside namespace Data, process() calls Data\process(). To call the global process(), prefix with a backslash: \process(). global\process() is invalid syntax. Data\process() calls the local function.