PHP OOP问题

Cli*_*ote 2 php oop

这是我的父类:

class Model extends CI_Model
{
    protected $table, $idField='id';
    public $fields;

    function __construct()
    {
        parent::__construct();
        $this->fields  = new ValFieldGroup();
    }
    //Other Code
}
Run Code Online (Sandbox Code Playgroud)

这是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;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我遇到错误的子类:

class User extends Model
{
    function __construct()
    {
        parent::__construct();

        $this->fields.add('first', 'required', 'First Name');
        // Snip
    }
    // Snip
}
Run Code Online (Sandbox Code Playgroud)

运行此代码时出现以下错误:

Fatal error: Call to undefined function add() in \models\user.php..
Run Code Online (Sandbox Code Playgroud)

这是班上的这一行User:

$this->fields.add('first', 'required', 'First Name');
Run Code Online (Sandbox Code Playgroud)

当我print_r($this->fields)在这行之前做一个时,我得到:

ValFieldGroup Object ( [fields:private] => Array ( ) )
Run Code Online (Sandbox Code Playgroud)

很明显,这个类正在设置但我不能使用我想要的功能..我做错了什么?

Gor*_*don 7

除非add是来自全球范围的功能,否则我fields.add()应该说fields->add().点是PHP中的字符串连接运算符.