Jump to content

Where is class Db defined?


wshyang

Recommended Posts

Hi guys :)

 

I've been wanting to create a add-on cron script that utilises Prestashop's DB class instead of instantiating the database handle directly, but I can't seem to figure out where did the "Db" class commonly referenced by "Db::getInstance()" calls get defined.

 

Before I get accused of not searching, I did actually read the other thread :)

 

However, as bizarre as it is, that file does not exist on our installation:

 

[/home/xxxx/www/shop]# find . | grep Db.php
./classes/Db.php
./admin1234/tabs/AdminDb.php

 

And even better:

 

[/home/xxxx/www/shop/classes]# grep -r "extends Db" ../
../classes/MySQL.php:class MySQLCore extends Db

 

And most intriguing!

 

[/home/xxxx/www/shop/override]# cd ../override/
[/home/xxxx/www/shop/override]# ls
./  ../  classes/  controllers/
[/home/xxxx/www/shop/override]# cd classes/
[/home/xxxx/www/shop/override/classes]# ls
./  ../  _FrontController.php*  _Module.php*  _MySQL.php*

 

Our shop is working, so obviously I am missing something. We are running Prestashop 1.4.1.

 

Edit:

 

One more grep through the code base found nothing:

 

 

grep -rwI "Db" . | grep -v "::"
./modules/productcomments/productcommentscriterion.php:require_once(dirname(__FILE__).'/../../classes/Db.php');
./classes/MySQL.php:class MySQLCore extends Db
./classes/Db.php:  * Get Db object instance (Singleton)
./classes/Db.php:  * @return object Db instance
./classes/Db.php:  * Build a Db object

Edited by wshyang (see edit history)
Link to comment
Share on other sites

It's declared in the source as:

 

abstract class DbCore {}

 

During initialisation - as part of the override system - the "proper" Db class will be instantiated which is why you can't find a declaration.

Link to comment
Share on other sites

  • 6 months later...

Hello Paul.

 

I came across this thread because I was looking for the definition of "class Contact" which I find instantiated in AdminContactsController.php. I find a file, classes/Contact.php, with a definition of class ContactCore, but no class Contact. I don't find any other Contact.php (such as in the override directory). I do not see any definition of a class named Contact anywhere.

 

I found a definition for class AdminContactsControllerCore in AdminContactsController.php and in its __construct() you find the following statement: "$this->className = 'Contact';"

 

I am new to both PrestaShop and PHP but I'd like to understand on a high level how this works. My guess is that in PHP has a mechanism to override an existing class name or "alias" a class that is not defined to instead instantiate some other class. In other words, when something like "new Contact(...)" appears in the source code, things can be configured to cause PHP to instantiate another class (say, ContactCore), perhaps through some registration process (at run time) or declaration process (at development time). This mechanism allows you to override what gets instantiated at run time for a specified class (here, Contact.). Is this what is happening? I can see the incredible power of such a mechanism (for good or for evil!)

 

Is this a PHP feature or a PrestaShop feature? Can you point me to a resource where it is documented?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...