Trait in CodeIgniter
Asked Answered
T

2

5

I'm trying to use traits in CodeIgniter. I put the Trait in the libraries directory and then loaded the library in the controller. Then I used the trait in the model, but it didn't work and I received the following error

Trait file (in library):

trait DataTablesTrait{
    public function query(){
        $this->db->select('*');
        $this->db->limit(10, 1);
        return $this->db->get('my_table');
    }
}
?>

Controller:

class myController extends CI_Controller {  

    public function __construct(){
        parent::__construct();
        $this->load->library('DataTablesTrait');
        $this->load->model('ABC_Model');
    }
} 

Model:

class ABC_Model extends CI_Model {
    use DataTablesTrait;
    function callQuery(){
        $this->query();
    }
}

I got this error:

Non-existent class: DataTablesTrait

Please advise

Tore answered 18/12, 2017 at 15:4 Comment(2)
If CodeIgniter doesn't feature an autoloader use include to include the file that contain the trait into the file that uses it. Or switch to a modern framework.Peonir
@Peonir Thank you. Include work fine. I can't switch to another framework, the system already built using Codeigniter.Tore
K
12

CodeIgniter (CI) isn't trait or namespace friendly but they can be used. It requires working around CI a bit though.

The main problem, the CI thing you have to work around, is the call

$this->load->library('DataTablesTrait');

CI will find this file but load->library will try to instantiate the trait which fails because it is not possible to instantiate a Trait on its own.

Replace the line above with

include APPPATH.'/libraries/DataTablesTrait.php';

You should be free of the error with that. But you're not going to get results because callQuery() does not return anything. To round out the test have callQuery() return the CI_DB_result object the Trait should produce

public function callQuery()
{
    return $this->query();
}

Add an index function to the controller so we can dump the output

public function index()
{
    $DB_result = $this->ABC_Model->callQuery();
    var_dump($DB_result->result());
}

This should produce the expected output - assuming that 'my_table' has data :)

Kneedeep answered 18/12, 2017 at 17:49 Comment(3)
Thank you :) This work fine: include APPPATH.'/libraries/DataTablesTrait.php';Tore
i found out by default system will load library from the system->library.. but this one helped me alotPashalik
This answer pertains to CodeIgniter 3.x and earlier. CI versions 4.x and newer do not require the workaround shown.Kneedeep
U
1

Worth saying that traits are now fully supported in Codeigniter 4 and can be implemented as you would expect using namespaces etc.

// TRAIT
namespace App\Controllers\traits;

trait YourTraitName {

}

// CONTROLLER
use App\Controllers\traits\YourTraitName;

class Admin extends BaseController {

    use YourTraitName;  
Ulrich answered 22/12, 2022 at 12:48 Comment(1)
And what about Codeigniter 3?Preface

© 2022 - 2024 — McMap. All rights reserved.