2014-04-03 123 views
0

我在擴展CI_Model的Codeigniter中有一個類。無法在PHP codeigniter Model類中訪問全局變量。

我已經聲明瞭一個全局數組$ data,但是我無法從我想要推送某些項目的函數中訪問這個數組。 表示

甲PHP錯誤遇到消息:未定義變量:數據

致命錯誤:無法在上線51訪問空屬性/api/system/core/Model.php

代碼:

class Subscription_collection_model extends CI_Model { 
     public $data=array(); 
    /* 
    |----------------------------------------- 
    | Constructor 
    |----------------------------------------- 
    */ 

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

    function get() { 

      $this->db->select('family.house_name,family.id,family_grade.amount'); 
      $this->db->from('family'); 
      $this->db->join('family_grade','family_grade.id = family.family_grade_id'); 
      $query = $this->db->get(); 
      if ($query->num_rows() > 0) 
       { 
       foreach ($query->result() as $row) 
        { 
         $this->findBalance($row->id); 
        } 
       }  
     return $data; 
     } 


    function findBalance($id) { 

     $this->db->where('family_id', $id); 
     $this->db->select_sum('collecting_amount'); 
     $query = $this->db->get('subscription_collection'); 

      if ($query->num_rows() > 0) 
       { 
       foreach ($query->result() as $row) 
        { 
         array_push($this->$data,$row->collecting_amount); 


        } 
       } 
    } 
} 
+2

'$回報數據;'應該是'$返回這 - > data'吧?也不是'array_push($ this - > $ data',但'array_push($ this-> data' – Eternal1

+0

k.thanks但是當我推$ query到$ data時顯示錯誤。如何推送$ query結果的每一行並推送findBalance()函數每行中的一個項目。 –

回答

1

你有一個錯字。您要撥打$this->data而不是$this->$data。 後者會假設,你想在實例上調用一個動態變量。

$prop = 'myProperty'; 
$this->$prop = 'test'; 

相同

$this->myProperty = 'test'; 
1

如何:

return $this->data;