2012-03-01 11 views
16

これは、CodeIgniterの初心者にとって非常によくある問題ですが、これまで見つかった解決策はどれも私の問題とはあまり関係がありません。トピックのように私はCodeIgniterにカスタムクラスを組み込もうとしています。CodeIgniterのカスタムクラス

私は下のクラスのいくつかのオブジェクトを作成して配列に配置しようとしています。したがって、モデルに使用できるクラスが必要です。

私は、CodeIgniter内でload(library-> load( 'myclass'))関数を使用しようとしましたが、モデルの外にあるクラスのオブジェクトを最初に作成しようとします。コンストラクタは、いくつかのパラメータを期待しています。

私がこれまでに見つけたのソリューションは、十分に細かい思われる

  1. 単純なPHPなどがありますが、私は CodeIgniterのに新しいですので、私は確信して私は「作りたいです も可能です。
  2. 提案したように "ラッパークラス"を作成するhereしかし、私はこれをどのように実装するのかは不明です。

私に含めるクラス、User.php

function get_all_users() 
{ 
    $query = $this->db->get('tt_login'); 
    $arr = array(); 

    foreach ($query->result_array() as $row) 
    { 
     $arr[] = new User 
     (
      $row['login_ID'], 
      $row['login_user'], 
      $row['login_super'], 
      $row['crew_type'], 
      $row['login_name'] 
     ); 
    }  

    return $arr; 
} 

そして最後にコントローラを必要と User.php

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 
class User{ 
    public $ID = 0; 
    public $username = 0; 
    public $access_lvl = 0; 
    public $staff_type = 0; 
    public $name = 0;  

    public function __construct($ID, $username, $access_lvl, $staff_type, $name) 
    { 
     $this->ID = $ID; 
     $this->username = $username; 
     $this->access_lvl = $access_lvl; 
     $this->staff_type = $staff_type; 
     $this->name = $name; 
    } 

    public function __toString() 
    { 
     return $this->username; 
    } 
} 
?> 

方法(モデル) 、

function index() 
{ 
     $this->load->library('user'); 
     $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(); 

答えて

関連する問題