Custom classes in CodeIgniter

前端 未结 6 555
走了就别回头了
走了就别回头了 2021-01-31 05:45

Seems like this is a very common problem for beginners with CodeIgniter, but none of the solutions I\'ve found so far seems very relevant to my problem. Like the topic says I\'m

6条回答
  •  隐瞒了意图╮
    2021-01-31 05:53

    If you have PHP version >= 5.3 you could take use of namespaces and autoloading features.

    A simple autoloader library in the library folder.

    
    

    The User object in the model dir. ( models/User.php )

    And instead of new User... new models\User ( ... )

    function get_all_users(){
        ....
        $arr[] = new models\User(
        $row['login_ID'],
        $row['login_user'],
        $row['login_super'],
        $row['crew_type'],
        $row['login_name']
        );
        ...
    }
    

    And in controller just make sure to call the customautoloader like this:

    function index()
    {
            $this->load->library('customautoloader');
            $this->load->model('admin/usersmodel', '', true);            
    
            // Page title
            $data['title'] = "Some title";
            // Heading
            $data['heading'] = "Some heading";
            // Data (users)
            $data['users'] = $this->usersmodel->get_all_users();
    

提交回复
热议问题