0
0
PHPprogramming~20 mins

Namespace declaration syntax in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Namespace Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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();
AHello from Utils! and Hello from Utils!
BHello from Utils! and Hello from Main!
CHello from Main! and Hello from Main!
DHello from Main! and Hello from Utils!
Attempts:
2 left
💡 Hint
Remember that the code after the second namespace declaration is in MyApp\Main namespace.
Predict Output
intermediate
1: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";
}
ANo error, outputs nothing
BFatal error: Cannot redeclare namespace
CParse error: syntax error, unexpected 'function' (missing semicolon after namespace)
DWarning: namespace not found
Attempts:
2 left
💡 Hint
Check the syntax after the namespace declaration line.
Predict Output
advanced
2: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;
}
A1,2
B2,1
CVALUE,VALUE
DParse error
Attempts:
2 left
💡 Hint
Constants in namespaces are accessed with backslash and namespace name.
Predict Output
advanced
2: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();
}
AInside Inner and Inside Outer
BInside Outer and Inside Inner
CFatal error: Class not found
DParse error
Attempts:
2 left
💡 Hint
Classes in different namespaces can have the same name.
Predict Output
expert
3: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();
}
AAlpha and Alpha\Beta
BAlpha\Beta and Alpha
CFatal error: Class not found
DParse error
Attempts:
2 left
💡 Hint
The use statement imports Alpha\Beta\Sample as BetaSample inside Alpha namespace.