2

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:

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:

$this->fields.add('first', 'required', 'First Name');

When I do a print_r($this->fields) before this line, I get:

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?

2
  • 1
    fields.add is not valid syntax in PHP - if you have a property named $fields, it'd have to be $this->fields->add Commented Apr 15, 2011 at 17:45
  • 1
    @Peka - Actually, it's valid syntax by pure chance. It concatenates the $this->fields property with the output of function add(). Commented Apr 15, 2011 at 17:50

3 Answers 3

7

Unless add is a function from the global scope, I'd say fields.add() should be fields->add(). The dot is the string concatenation operator in PHP.

Sign up to request clarification or add additional context in comments.

1 Comment

I tend use periods as member operator all the time xD
2

Your User class extends SuperModel, not Model.

Comments

2

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.