简体   繁体   中英

PHP use class in global namespace

I have a DB wrapper class that uses PDO and in the constructor I create a PDO object. The wrapper class is in our namespace and we are using an autoloader. The issue is that the PDO class cannot be found within our namespace, so I tried using the global namespace as described here .

//Class file
namespace Company\Common;
class DB {
    private function __construct(){
        $this->Handle=new PDO(...);
    }
}

With this, I get this (as expected):

Warning: require(...\vendors\Company\Common\PDO.class.php): failed to open stream

If I do this:

namespace Company\Common;
use PDO;

I get this:

Fatal error: Class 'DB' not found in ...\includes\utils.php

And utils.php contains this on the error line, which worked fine before implementing namespaces:

DB::getInstance();

Alternatively I tried this:

namespace Company\Common;
class DB {
    private function __construct(){
        $this->Handle=new \PDO(...);
    }
}

Which tried to load the PDO class within our namespace as it originally did.

How can I resolve this? I thought by doing use PDO or new \\PDO it would load the global PDO class, but it doesn't seem to be working?

In Namespaced PHP, references to a class must include the namespace of that class, unless you have a use statement that includes that class or part of its namespace.

So, if you have no use statement for it, then PDO and other global classes must be referenced with the leading backslash -- ie $obj = new \\PDO();

If you have a use statement that references that class, then you may reference it by just the classname:

use PDO;
....
$obj = new PDO();

If you're referencing a lot of global classes, you'll need to use each of them individually if you want to avoid using the backslash every time.

Normally in our projects we do set include path like this:

set_include_path('PATH_TO_GLOBAL_LIBS; PATH_TO_LIBRARY_1; PATH_TO_LIBRARY_X; PATH_TO_DOCUMENT_ROOT');

By this we tell PHP (Apache) to search these paths for any classes that should be included by autoloader.

Then assuming we have some Library_1 in /var/www/Libs/library_1 and that this path is added to the include_path we could do this:

namespace Company\Common;

Class DB {
    private function __construct() {
        $this->Handle = new \Library_1();
    }
}

which should be the same as

namespace Company\Common;

use \Library_1;

Class DB {
    private function __construct() {
        $this->Handle = new Library_1();
    }
}

This works, your problem is elsewere.

index.php

<?php
include('_db.php');
use Company\Common\DB;
new DB;

_db.php

<?php
namespace Company\Common;
use \PDO;

class DB {
    public function __construct() {
        $this->Handle = new PDO;
    }
}

To access global objects like PDO and DateTime , you need to prefix them with a backslash. So you then have two options.

First, either use the classes you wish to use in your name-spaced class file:

<?php
namespace Vendor;

use \PDO;
use \PDOException;

class MyClass
{
    public function __construct()
    {
        try {
            $db = new PDO($dsn, $user, $pass);
            $db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
        }
        catch (PDOException $e) {
            echo $e->getMessage();
            exit;
        }
    }
}

Or just use the backslash-prefixed declarations within your class:

<?php
namespace Vendor;

class MyClass
{
    public function __construct()
    {
        try {
            $db = new \PDO($dsn, $user, $pass);
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }
        catch (\PDOException $e) {
            echo $e->getMessage();
            exit;
        }
    }
}

Personally, I prefer the first approach.

Solved it. I didn't realize that aliasing a namespace only applies to the current file, and not any future included files. Found this on PHP.net which also applies to aliasing:

Importing rules are per file basis, meaning included files will NOT inherit the parent file's importing rules.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM