Reputation: 45
I have made a __autoload function like this(in autoload.php):
function __autoload($name) {
$arrName = explode('_', $name);
$typename = strtolower(array_shift($arrName));
$moduleName = strtolower(array_shift($arrName));
if(count($arrName) > 0 ){
$className = strtolower(array_shift($arrName));
} else {
$className = $moduleName;
}
$location = '' . $typename . '/' . $moduleName . '/' . $className . '.php';
include_once($location);
}
(I'm working with MVC(Model View Controller) structure).
And I'm calling a "new PDO"(in database.php):
private static $pdo;
private static function getDB() {
self::$pdo = new PDO("mysql:host=localhost;dbname=something",'something','something');
return self::$pdo;
}
It gives me an error like this:
Warning: include_once(pdo//.php): failed to open stream: No such file or directory in controller/autoload/autoload.php on line 15
Warning: include_once(): Failed opening 'pdo//.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in controller/autoload/autoload.php on line 15
Fatal error: Class 'PDO' not found in controller/core/database.php on line 7
I have tried using "new \PDO" but that doesn't change anything. I also tried using "use PDO" but that gives me some more errors, maybe I use it wrong.
Thanks.
Upvotes: 0
Views: 227
Reputation: 12826
I've explained what's going on in the comments:
// This gives array(1) { [0]=> string(3) "PDO" }
$arrName = explode('_', $name);
// This gives string(3) "pdo"
$typename = strtolower(array_shift($arrName));
// This gives string(0) ""
$moduleName = strtolower(array_shift($arrName));
// This returns FALSE
if (count($arrName) > 0 )
{
$className = strtolower(array_shift($arrName));
}
else
{
// This gives string(0) ""
$className = $moduleName;
}
// This gives string(9) "pdo//.php"
$location = '' . $typename . '/' . $moduleName . '/' . $className . '.php';
However, as I assume you're using (or at least trying to use) the PHP Data Objects
extension, you need to make sure that it is installed.
Upvotes: 0