请看我的AN EMPTY ARRAY
代码。有谁能告诉我为什么在执行下面的代码时,数组是空的,并且没有从AccountModel
中填充$accountModel->setEmail('[email protected]');
class AccountModel extends Model {
protected $table = 'account',
$key = 'id',
$data = array(
'id' => null,
'email' => null,
'first_name' => null,
'created' => null,
'last_login' => null,
'hash' => null
);
}
class Model extends CI_Model {
protected $table = null;
protected $key = null;
protected $connection = null;
protected $data = array();
public function __call ($function, $arguments) {
$original = $function;
$function = strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $function));
$prefix = substr($function, 0, 4); //returns get_ or set_
if ($prefix == 'get_' || $prefix == 'set_') {
$key = substr($function, 4); //returns "email"
var_dump($this->data); //AN EMPTY ARRAY
exit($key);
}
throw new Exception('Call to undefined method '.get_class($this).'::'.$original.'()');
}
}
根据example 2 on PHP.net的说法,它应该工作得很好。
我正在尝试运行$accountmodel->setemail('[email protected]');
更新:我意识到我的代码中还有一个bug,失败了。谢谢你们的帮助
最佳答案
似乎,您的$accountmodel不属于accountmodel类:尝试var_dump(get_class($this))
<?php
class Animal {
public $data = 'i`m animal';
protected $_data = 'i`m animal';
public function go() {
var_dump($this->data);
var_dump($this->_data);
}
public function __call ($function, $arguments) {
echo 'I was called as ' . $function . PHP_EOL;
var_dump($this->data);
var_dump($this->_data);
}
}
class Cat extends Animal {
public $data = 'i`m cat';
protected $_data = 'i`m cat';
}
$pussy = new Cat();
$pussy->go();
$pussy->iamMagicMethod();