How can the use of multiple namespaces with the same class name lead to conflicts in PHP code?
When using multiple namespaces with the same class name in PHP, conflicts can arise because PHP does not allow for two classes with the same name to be loaded at the same time. To solve this issue, you can use the fully qualified class name when referencing the class in your code, specifying the namespace along with the class name.
<?php
namespace Namespace1 {
class MyClass {
public function __construct() {
echo "Namespace1 MyClass";
}
}
}
namespace Namespace2 {
class MyClass {
public function __construct() {
echo "Namespace2 MyClass";
}
}
}
$object1 = new \Namespace1\MyClass();
$object2 = new \Namespace2\MyClass();