Symfony 7 – PHP – Namespaces

20 multiple-choice questions about PHP namespaces, including basic concepts, resolution rules, and practical code examples.


Question 1: What problem do namespaces solve in PHP?

Question 2: What keyword defines a namespace in PHP?

Question 3: Where should the namespace declaration appear in a PHP file?

Question 4: How do you reference a class from the global namespace inside another namespace?

Question 5: Given namespace App\Controller; what is the fully qualified name of class Home?

Question 6: What does the use statement do?

Question 7: What happens if two imported classes have the same short name?

Question 8: In the line use MyApp\Services\Mailer as Mail; what does Mail represent?

Question 9: What is the correct way to call a class from another namespace without use?

Question 10: Consider: namespace A; class X {}; namespace B; class Y extends \A\X {}; Which X does Y extend?

Question 11: If you call new Helper() inside namespace App\Utils, which class will PHP look for first?

Question 12: Which statement about subnamespaces is TRUE?

Question 13: What will echo out in this code?\nnamespace Test; function hello(){return "Hi";} echo hello();

Question 14: Can constants be namespaced in PHP?

Question 15: What does the use function syntax (PHP 5.6+) allow?

Question 16: Given use MyLib\Utils\Helper; and class Helper defined locally, which is used?

Question 17: Can you declare multiple namespaces in one PHP file?

Question 18: Which namespace is used when no namespace is declared?

Question 19: What does the namespace separator (\) indicate to PHP?

Question 20: Which output is correct?\nnamespace App; use DateTime; echo (new DateTime())->format("Y");