回答

0

爲什麼不創建與函數庫,並自動加載該庫 另一方面,您可以在子文件夾中創建一個類,如

<?php 
class My_super_class{ 

    protected $CI; 

    public function __construct(){ 
     parent::__construct(); 

     $this->CI =& get_instance(); 
    } 

    public function do_somthing($param1 = NULL){ 
     //param1 as array 
     $this->CI->db->insert('table', $param1); 
    } 

    public function check_login(){ 
     $this->CI->load->library('session'); 

     return (! empty($this->CI->session->userdata('id'))) ? TRUE : FALSE; 
    } 
} 
?> 
+0

嗨,我知道如何創建圖書館,這也是即時通訊考慮..埠TI認爲我可以直接使用db類,而無需調用CI實例..我目前正在閱讀數據庫類以及如何手動實例化..我需要從單獨文件手動調用它的原因是.. ..裏面的文件文件夾會很大..我主要只關注數據庫操作.. – user2070715

2

實現您的目標的正確方法是使其成爲幫助文件。

/application/helpers/function1.php

function1.php

<?php 
if(!function_exists('function1')) 
{ 
    function function1() 
    { 
     // Get the CodeIgniter instance by reference 
     // Basically, $this from the controller is now $CI within this function 
     $CI = &get_instance(); 

     $CI->db->query(""); 
     // do whatever 

     return 'hi'; 
    } 
} 

所以每當你需要在你的控制器,然後就去做:

class Welcome extends CI_controller { 

    public function __construct() 
    { 
     $this->load->helper('function1'); 
    } 

    public function index() 
    { 
     echo function1(); 
    } 
} 
相關問題