PHP use class in global namespace

前端 未结 5 1435
长情又很酷
长情又很酷 2021-01-21 18:18

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

5条回答
  •  无人共我
    2021-01-21 18:56

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

提交回复
热议问题