Namespaces help prevent conflicts by allowing the same class or function names to exist in different parts of a program without clashing.
<?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(); }
Each namespace defines its own Test class, so both can exist and be used separately.
<?php namespace A; class Test {} namespace B; class Test {} namespace { $obj = new Test(); }
The code tries to create an object of class Test without specifying a namespace, but no global Test class exists.
Inside a namespace, an unqualified class name like 'User' resolves to MyApp\User. Option B is correct; A refers to MyApp\MyApp\User, B has a syntax error (missing semicolon), and C refers to the global User.
Namespaces prevent class name collisions by allowing identical class names to exist in separate namespaces, avoiding overwriting or errors.
