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
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();
}
}
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.
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.
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;
}
}
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.