CodeIgniter Forums
Replacing Core Classes Note needed - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: CodeIgniter 3.x (https://forum.codeigniter.com/forumdisplay.php?fid=17)
+--- Thread: Replacing Core Classes Note needed (/showthread.php?tid=68512)



Replacing Core Classes Note needed - Martin7483 - 07-20-2017

Hi,

Maybe it is a good idea to have a note added to the documentation regarding the replacement/extention of core Database classes.

https://www.codeigniter.com/userguide3/general/core_classes.html#replacing-core-classes

It is misleading as you would now expect it to be possible to replace/extend Database classes


RE: Replacing Core Classes Note needed - Narf - 07-20-2017

There is a note where it is appropriate: https://www.codeigniter.com/userguide3/general/creating_libraries.html

Core classes are the ones located under system/core/, which the DB libs are not.


RE: Replacing Core Classes Note needed - Martin7483 - 07-20-2017

I knew I had seen it somewhere, just couldn't remenber where

Adding that same note on this page can't hurt

https://www.codeigniter.com/userguide3/g...re-classes

Seeing as DB libs are also not located in ./system/libraries/


RE: Replacing Core Classes Note needed - spjonez - 07-20-2017

You can extend the core DB libraries if you wish.

application/core/MY_Loader.php:

Code:
    /* overloaded methods */

    public function database( $params = '', $return = false, $query_builder = null ) {
        $ci =& get_instance( );

        if ( $return === false && $query_builder === null && isset( $ci->db ) && is_object( $ci->db ) && !empty( $ci->db->conn_id) ) {
            return false;
        }

        require_once( BASEPATH . 'database/DB.php' );

        $db =& DB( $params, $query_builder );

        $driver = config_item( 'subclass_prefix' ) . 'DB_' . $db->dbdriver . '_driver';
        $file = APPPATH . 'libraries/' . $driver . '.php';

        if ( file_exists( $file ) === true && is_file( $file ) === true ) {
            require_once( $file );

            $dbo = new $driver( get_object_vars( $db ) );
            $db = & $dbo;
        }

        if ( $return === true ) {
            return $db;
        }

        $ci->db = '';
        $ci->db = $db;

        return $this;
    }


application/libraries/MY_DB_mysqli_driver.php:

Code:
<?php if ( !defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );

class MY_DB_mysqli_driver extends CI_DB_mysqli_driver {
}

?>



RE: Replacing Core Classes Note needed - Martin7483 - 07-20-2017

But you can't do that by default. You first need to extend the Loader

And when extending the Loader, or any other class, all you need to do is create a MY_xxxxx.php variant of the file and store it in the correct directory


RE: Replacing Core Classes Note needed - spjonez - 07-20-2017

(07-20-2017, 06:23 AM)Martin7483 Wrote: But you can't do that by default. You first need to extend the Loader

Correct, the documentation states this Narf linked where above. That doesn't mean you can't only that it isn't available out of the box. The code I posted will allow you to extend them without modifying any core files.