Have encountered an issue I can't seem to figure out now by myself.
Using Symfony autoload module.
Here's my factory:
namespace Core\Factories;
use \Core\Gateway;
class DatabaseAccessFactory {
// Define client type
const DEF = 'mongo';
public function createObject($type) {
switch($type) {
case self::DEF:
return new MongoClientGateway();
break;
default:
return false;
}
}
}
Example of /Core/Gateway/MongoClientGateway.php
<? namespace Core\Gateway;
class MongoClientGateway implements MongoDbGateway {
public function setUp(){
}
public function query(){
}
public function save(){
}
}
So, basically I'm using "use" keyword to load gateway namespace into my current one, and then I try to instantiate a class that is under \\Core\\Gateway namespace, but it says class is not found. Am I missing something?
You need to specifcy the class as well
use Core\Gateway\MongoClientGateway
or access the class with the namespace you used
new Gateway\MongoClientGateway
Btw, there's no need for the first "\\" in use \\Core\\Gateway
use Foo\\Bar
, without leading backslash. use Foo\\Bar
does not mean that every Class
implicitly resolves to Foo\\Bar\\Class
now. use Foo\\Bar
is shorthand for use Foo\\Bar as Bar
, so you can reference the namespace Foo\\Bar
using merely Bar
. use
is not "importing a namespace" , it's aliasing a namespace to a shorter name. Gateway\\MongoClientGateway
, or use Core\\Gateway\\MongoClientGateway
explicitly if you want to be able to write just MongoClientGateway
. you used "use" wrong. waht "use" does, is to tell your code where class comes from.
sample code:
use \my\namespace\className
new ClassName();
this will make the className accassible without a namespace.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.