Reputation:
Am learning how namespace works and have three files:- index, iClass, and iClassed
iClass.php
<?php
namespace app\ic;
class iClass {
public static function callMeFromClass() {
echo 'OK - you have called me!';
exit;
}
}
iClassed.php
<?php
namespace app\icl;
class iClass {
public static function callMe() {
echo 'OK - iclassed - you have called me!';
exit;
}
}
and the index.php
<?php
namespace inex;
require_once 'iClass.php';
require_once 'iClassed.php';
use app\ic\iClass;
iClass::callMeFromClass();
use app\icl\iClass;
iClass::callMe();
The error that I get after I try to run is
Cannot use app\icl\iClass as iClass because the name is already in use in C:\xampp\htdocs\namespace\index.php on line 10
Can somebody explain why the error.
Upvotes: 0
Views: 45
Reputation: 42915
Sure, you try to import two classes under the same local name iClass, that obviously is not possible. Either use the fully qualified class name (namespaced name) or import them under different names.
Using a fully qualified class name would mean:
\app\ic\iClass::callMe();
\app\icl\iClass::callMe();
Or you "use" the classes under a different local name:
use app\ic\iClass;
use app\icl\iClass as iClassed;
iClass::callMeFromClass();
iClassed::callMe();`
I suggest you start reading the documentation of the things you use. It is of great quality and comes with good examples: http://php.net/manual/en/language.namespaces.php
Upvotes: 1