0
0
PHPprogramming~20 mins

Global namespace access in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Global Namespace Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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");
?>
A
MyApp strlen: 4
4
4
B
MyApp strlen: 4
4
MyApp strlen: 4
C
4
4
MyApp strlen: 4
D
4
MyApp strlen: 4
4
Attempts:
2 left
💡 Hint
Remember that \strlen calls the global function, while strlen inside namespace calls the local one.
Predict Output
intermediate
2: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";
?>
A
3.14
Fatal error: Uncaught Error: Undefined constant PI
B
3.14
PHP Notice: Undefined constant PI
C
3.14
1
D
3.14
3.14
Attempts:
2 left
💡 Hint
Global constants are not automatically imported into namespaces.
🔧 Debug
advanced
2: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();
?>
ACalling test() fails because it requires parentheses.
BCalling test() fails because it is ambiguous inside the namespace.
CCalling \test() fails because global functions cannot be called inside namespaces.
DCalling \test() fails because there is no global function named test.
Attempts:
2 left
💡 Hint
Check if the global namespace has a function named test.
📝 Syntax
advanced
2: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 = ???;
?>
A$logger = new \Logger();
B$logger = new Logger();
C$logger = new \App\Logger();
D$logger = new \global\Logger();
Attempts:
2 left
💡 Hint
Use a leading backslash to access global classes from inside a namespace.
🚀 Application
expert
3: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();
?>
Aprocess()
Bglobal\process()
C\process()
DData\process()
Attempts:
2 left
💡 Hint
Use a leading backslash to call global functions inside namespaces.