4

I'm new to PHP namespace. and there is a problem when I use auto-load.

ROOT/Application/Instance.php

<?php

namespace Application;

class Instance {

    public static $_database;

    public function __construct() {
        self::$_database = new \Application\Module\Database();
    }

    public static function database() {
        return self::$_database;
    }

    public static function ID(){
        return md5(uniqid(mt_rand(), TRUE) . mt_rand() . uniqid(mt_rand(), TRUE));
    }

    public static function autoload($_className) {
        $thisClass = str_replace(__NAMESPACE__.'\\', '', __CLASS__);
        $baseDir = __DIR__;
        if (substr($baseDir, -strlen($thisClass)) === $thisClass) {
            $baseDir = substr($baseDir, 0, -strlen($thisClass));
        }
        $_className = ltrim($_className, '\\');
        $fileName  = $baseDir;
        $namespace = '';
        if ($lastNsPos = strripos($_className, '\\')) {
            $namespace = substr($_className, 0, $lastNsPos);
            $_className = substr($_className, $lastNsPos + 1);
            $fileName  .= str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
        }
        $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $_className) . '.php';
        if (file_exists($fileName)) {
            require $fileName;
        }
    }

    public static function registerAutoloader() {
        spl_autoload_register(__NAMESPACE__ . "\\Instance::autoload");
    }

}

ROOT/Application/Module/Database.php

<?php

namespace Application\Module;

include 'FluentPDO/FluentPDO.php';

class Database extends Module {

    public static $_instance;

    public function __construct() {
        if(self::$_instance === NULL) {
            self::$_instance = new FluentPDO(new PDO("mysql:host=8273639.mysql.rds.aliyuncs.com;dbname=db", 'name', 'password'));
        }
    }

}

When I run this:

new \Application\Instance();

I got this error:

Fatal error: Class 'Application\Module\FluentPDO' not found in /mnt/www/airteams_com/public/Application/Module/Database.php on line 13

I'm pretty sure that 'FluentPDO/FluentPDO.php' exists. and the error shows a wrong path of the file. the right path is 'ROOT/Application/Module/FluentPDO/FluentPDO.php'

So how can i use a no namespace class in my situation? thanks.

1 Answer 1

1

When you are working with namespaces you must fully qualify each class unless it's a child of the current namespace.

As such the FluentPDO is probably on the root namespace which means you need to access it like such:

self::$_instance = new \FluentPDO(new \PDO("mysql:host=8273639.mysql.rds.aliyuncs.com;dbname=db", 'name', 'password'));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.