Challenge - 5 Problems
Namespace Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this PHP code with namespaces?
Consider the following PHP code. What will it output when run?
PHP
<?php namespace MyApp\Utils; function greet() { return "Hello from Utils!"; } namespace MyApp\Main; function greet() { return "Hello from Main!"; } use function MyApp\Utils\greet as utilsGreet; echo greet() . " and " . utilsGreet();
Attempts:
2 left
💡 Hint
Remember that the code after the second namespace declaration is in MyApp\Main namespace.
✗ Incorrect
The first greet() call is in MyApp\Main namespace, so it calls MyApp\Main\greet(). The second call uses the alias utilsGreet which points to MyApp\Utils\greet().
❓ Predict Output
intermediate1:30remaining
What error does this PHP namespace code produce?
What error will this PHP code produce when run?
PHP
<?php namespace MyApp\Tools; function tool() { return "Tool function"; }
Attempts:
2 left
💡 Hint
Check the syntax after the namespace declaration line.
✗ Incorrect
The namespace declaration line must end with a semicolon or a block. Missing semicolon causes a parse error.
❓ Predict Output
advanced2:00remaining
What is the output of this PHP code with multiple namespace declarations?
What will this PHP code output when executed?
PHP
<?php namespace A { const VALUE = 1; } namespace B { const VALUE = 2; } namespace { echo A\VALUE . "," . B\VALUE; }
Attempts:
2 left
💡 Hint
Constants in namespaces are accessed with backslash and namespace name.
✗ Incorrect
The code prints the VALUE constant from namespace A and then from namespace B separated by a comma.
❓ Predict Output
advanced2:30remaining
What is the output of this PHP code using namespace and class?
What will this PHP code output when run?
PHP
<?php namespace Outer\Inner; class Test { public function message() { return "Inside Inner"; } } namespace Outer; class Test { public function message() { return "Inside Outer"; } } namespace { $obj1 = new Outer\Test(); $obj2 = new Outer\Inner\Test(); echo $obj1->message() . " and " . $obj2->message(); }
Attempts:
2 left
💡 Hint
Classes in different namespaces can have the same name.
✗ Incorrect
The code creates objects from two different namespaces and calls their message methods, printing both results.
❓ Predict Output
expert3:00remaining
What is the output of this PHP code with nested namespaces and use statements?
What will this PHP code output when executed?
PHP
<?php namespace Alpha\Beta { class Sample { public function getName() { return "Alpha\\Beta"; } } } namespace Alpha { use Alpha\Beta\Sample as BetaSample; class Sample { public function getName() { return "Alpha"; } public function getBetaName() { $obj = new BetaSample(); return $obj->getName(); } } } namespace { $obj = new Alpha\Sample(); echo $obj->getName() . " and " . $obj->getBetaName(); }
Attempts:
2 left
💡 Hint
The use statement imports Alpha\Beta\Sample as BetaSample inside Alpha namespace.
✗ Incorrect
The Alpha\Sample class calls its own getName() and also creates an object of BetaSample (which is Alpha\Beta\Sample) to get its name.