Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fatal error: Class 'App\PDO' not found in

I get this error when I try to use namespaces.

I have

namespace App;

the top, and the class looks like

class database{

  function __construct(..)
    try{
      $this->db = new PDO(...)   <-- here the error
    ...
  }
}

I dont' understand how do namespaces work? Shouldn't PHP fallback to the default PDO class if app/PDO is not found?

like image 907
Alex Avatar asked May 24 '26 23:05

Alex


2 Answers

Shouldn't PHP fallback to the default PDO class if app/PDO is not found?

No, it should not.

From the documentation:

Class names always resolve to the current namespace name. Thus to access internal or non-namespaced user classes, One must refer to them with their fully qualified Name

For your specific example, the fully qualified name for PDO would be \PDO.

Just add use PDO; after the namespace and before the class.

like image 44
Jo Smo Avatar answered May 26 '26 12:05

Jo Smo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!