[英]Cannot use __get / __set function in CodeIgniter
I have a class inherited from CI_Model as follows: 我有一个从CI_Model继承的类,如下所示:
<?php
class DBObject extends CI_Model {
// fields
public $errorMsg;
protected $_columnsConfig;
protected $_tableName;
// functions
// constructor
function __construct() {
parent::__construct();
$this->load->database();
}
// getter
public function __get($name) {
if (strtolower($name) == "columncount") {
return count($this->_columnsConfig);
} else {
$colCfg = null;
$i = 0;
while ($i < count($this->_columnsConfig)) {
$dbcc = $this->_columnsConfig[$i];
if (strtolower($name) == strtolower($dbcc->propertyName)) {
$colCfg = $dbcc;
break;
}
$i++;
}
if ($colCfg != null) return $colCfg->storedValue;
else return null;
}
}
// setter
function __set($name, $value) {
$colCfg = null;
$i = 0;
while ($i < count($this->_columnsConfig)) {
$dbcc = $this->_columnsConfig[$i];
if (strtolower($name) == strtolower($dbcc->propertyName)) {
$colCfg = $dbcc;
break;
}
$i++;
}
if ($colCfg != null) $colCfg->newValue = $value;
}
}
When I run my code, I have an error message: 运行代码时,出现错误消息:
Type: Error
类型:错误
Message: Call to a member function database() on null
消息:在null上调用成员函数database()
Filename: C:\\xampp\\htdocs\\hostelry_pms\\application\\models\\DBObject.php
文件名:C:\\ xampp \\ htdocs \\ hostelry_pms \\ application \\ models \\ DBObject.php
Line Number: 11
行号:11
But when I comment the __get
/ __set
functions, the code can run without problem. 但是,当我注释
__get
/ __set
函数时,代码可以正常运行。
Any idea? 任何想法?
CI_Model
has its own __get
function already defined . CI_Model
已经定义了自己的__get
函数。
By overriding it (and totally changing how it works), chances are you've broken everything in CI that expects that magic method to work a certain way. 通过覆盖它(并完全改变它的工作方式),您很有可能破坏了CI中所有希望该魔术方法以某种方式起作用的东西。
$this->load
is probably one of those now-broken things. $this->load
可能是这些现在已经中断的事情之一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.