Problems with PHP namespaces and built-in classes, how to fix?

前端 未结 3 1202
后悔当初
后悔当初 2020-12-01 15:59

I\'m writing a small library in PHP and i\'m having some problems with built-in classes not being read. For example:

namespace Woody;

class Test {
  public          


        
3条回答
  •  萌比男神i
    2020-12-01 16:33

    The below should work:

    namespace Woody;
    
    class Test {
        public function __construct() {
            $db = new \PDO(params);
        }
    }
    

    You need to prefix PDO with the backslash so PHP knows it's in the global namespace.

提交回复
热议问题