0
0
PHPprogramming~20 mins

Why namespaces are needed in PHP - Challenge Your Understanding

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!
🧠 Conceptual
intermediate
1:30remaining
Why use namespaces in PHP?
Which of the following best explains why namespaces are needed in PHP?
ATo encrypt PHP code so it cannot be read by others.
BTo make PHP code run faster by grouping functions together.
CTo automatically load classes from external libraries without manual includes.
DTo avoid name conflicts between classes or functions with the same name in different parts of a program.
Attempts:
2 left
💡 Hint
Think about what happens if two parts of a program use the same class name.
Predict Output
intermediate
2:00remaining
Output with and without namespaces
What will be the output of this PHP code?
PHP
<?php
namespace A;
class Test {
  public function say() {
    return 'Hello from A';
  }
}

namespace B;
class Test {
  public function say() {
    return 'Hello from B';
  }
}

namespace {
  $a = new \A\Test();
  $b = new \B\Test();
  echo $a->say() . ' and ' . $b->say();
}
AHello from A and Hello from B
BHello from A and Hello from A
CHello from B and Hello from B
DFatal error: Cannot redeclare class Test
Attempts:
2 left
💡 Hint
Look at how the classes are called with their full namespace paths.
🔧 Debug
advanced
1:30remaining
Identify the error caused by missing namespace
What error will this PHP code produce?
PHP
<?php
namespace A;
class Test {}

namespace B;
class Test {}

namespace {
  $obj = new Test();
}
AParse error: syntax error
BNo error, creates an object of class A\Test
CFatal error: Class 'Test' not found
DNo error, creates an object of class B\Test
Attempts:
2 left
💡 Hint
Consider which namespace the code is running in when creating the object.
📝 Syntax
advanced
1:30remaining
Correct namespace usage syntax
Which option shows the correct way to declare and use a namespace in PHP?
A
&lt;?php
namespace MyApp;
class User {}
$user = new MyApp\User();
B
&lt;?php
namespace MyApp;
class User {}
$user = new User();
C
&lt;?php
namespace MyApp
class User {}
$user = new User();
D
&lt;?php
namespace MyApp;
class User {}
$user = new \User();
Attempts:
2 left
💡 Hint
Unqualified class names inside a namespace resolve to classes in that namespace.
🚀 Application
expert
2:30remaining
How namespaces solve real-world conflicts
Imagine two large PHP libraries both define a class named Logger. Without namespaces, what problem occurs and how do namespaces solve it?
AWithout namespaces, the Logger class from the second library overwrites the first, causing errors. Namespaces keep them separate so both can be used.
BWithout namespaces, PHP automatically merges both Logger classes into one. Namespaces prevent this merging.
CWithout namespaces, PHP runs slower. Namespaces speed up execution by grouping classes.
DWithout namespaces, Logger classes are ignored. Namespaces make PHP recognize them.
Attempts:
2 left
💡 Hint
Think about what happens when two classes have the same name in the same program.