Welcome Guest, Not a member yet? Register   Sign In
Trait in CodeIgniter
#1

(This post was last modified: 02-06-2023, 02:14 AM by HanogThamers.)

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 echatspin echatrandom 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
Reply
#2

You need to use namespaces correctly.
See https://www.w3schools.com/php/php_namespaces.asp
Reply




Theme © iAndrew 2016 - Forum software by © MyBB