There is an index.php file, which lies in the root, it connects a file with a connection to the database, so that it can be seen in this file:

require_once(ROOT.'/components/Db.php'); 

Here is the file Db.php:

 class Db { public static function getConnection() { $paramsPath = ROOT . '/config/db_params.php'; $params = include($paramsPath); $dsn = "mysql:host={$params['host']};dbname={$params['dbname']};charset=UTF8"; $db = new PDO($dsn, $params['user'], $params['password']); return $db; } } 

There is a file (lorem.php) that makes requests to the database, it has a connection to the database:

 $db = Db::getConnection(); 

And when I'm already in another file (ipsum.php) I include lorem.php, with:

 require_once("/lorem.php"); 

Then the problem arises in it, it gives an error: Fatal ERROR: Class 'Db' not found in D: \ wamp \ www \ models \ lorem.php Although if you track the transmission chain of the class Db: Db.php -> index.php - > lorem.php -x-> ipsum.php then everything should be fine. Tell me, what's the problem?

  • specify: 1) how the lorem.php file is associated with index.php (more precisely with db.php). 2) Do the queries work in the lorem.php file itself? Alex
  • @Alex Lorem.php is associated with index.php because index is the front controller, the single entry point. All that is written there is visible in the entire file. In index.php, the file with the Db class is called. And lorem.php refers to the class Db called in index.php. 2) Requests work everywhere fine, except for this file (ipsum.php). - Bim Bam
  • if "index is a front controller, a single entry point and Everything written there is visible in the whole file", then "require_once (" / lorem.php ");" does not affect the visibility of Db, because in lorem.php there is no database file included. Here you need require_once ("/ ipsum.php"); in index.php or in some file connected to it. - Alex

0