我在Codeigniter中有一个扩展CI_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);
}
}
}
}
你那里有一个错字。 你想打电话$this->data
而不是$this->$data
.后者假设您希望在实例上调用动态变量。
$prop = 'myProperty';
$this->$prop = 'test';
与
$this->myProperty = 'test';
怎么样:
return $this->data;