[英]PHP OOP Problem
This is my parent class: 这是我的父类:
class Model extends CI_Model
{
protected $table, $idField='id';
public $fields;
function __construct()
{
parent::__construct();
$this->fields = new ValFieldGroup();
}
//Other Code
}
Here's the code of the ValFieldGroup class: 这是ValFieldGroup类的代码:
class ValFieldGroup
{
private $fields = array();
public function add($name, $rules = '', $label = '')
{
$this->fields[$name] = new ValField($name, $rules, $label);
}
public function __get($name)
{
if (isset($this->fields[$name]))
return $this->fields[$name];
else
return false;
}
public function __set($name, $value)
{
$this->fields[$name] = $value;
}
public function getAll()
{
return $this->fields;
}
}
Here's the child class where I'm getting an error: 这是我遇到错误的子类:
class User extends Model
{
function __construct()
{
parent::__construct();
$this->fields.add('first', 'required', 'First Name');
// Snip
}
// Snip
}
I'm getting the following error when I run this code: 运行此代码时出现以下错误:
Fatal error: Call to undefined function add() in \models\user.php..
Which is this line in the class User
: 这个类在User
类中是哪一行:
$this->fields.add('first', 'required', 'First Name');
When I do a print_r($this->fields)
before this line, I get: 当我在这行之前做一个print_r($this->fields)
,我得到:
ValFieldGroup Object ( [fields:private] => Array ( ) )
So clearly the class is being setup but I can't use the function I want.. what am I doing wrong? 很明显,这个类正在设置但我不能使用我想要的功能..我做错了什么?
Unless add
is a function from the global scope, I'd say fields.add()
should be fields->add()
. 除非add
是全局范围内的函数,否则我认为fields.add()
应该是fields.add()
fields->add()
。 The dot is the string concatenation operator in PHP. 点是PHP中的字符串连接运算符。
您的User类扩展了SuperModel,而不是Model。
You aren't calling the method correctly, try this: 你没有正确调用方法,试试这个:
class User extends Model
{
function __construct()
{
parent::__construct();
$this->fields->add('first', 'required', 'First Name');
// Snip
}
// Snip
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.