Variables do not overwrite in parent class

Check out my code for AN EMPTY ARRAY. Can someone tell me why this array is empty and not populated from AccountModelwhen executing the following code

$accountModel->setEmail('test@test.com');

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.'()');
}
}

According to example 2 on PHP.net, it should work fine.

I am trying to run $ accountModel-> setEmail (' test@test.com ');

update: I understood one more error in my code, for my part it didn’t work. Thanks for helping the guys

+3
source share
4 answers

It seems your $ accountModel does not belong to the AccountModel class: try something like 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();
+2
source

. Model $data. "" $data AccountModel, - . , .

0

$data AccountModel . , . :

class AccountModel extends Model {
    public function __construct()
    {
        $this->table = 'account';
        $this->key = 'id';
        $this->data = array(
            'id' => null,
            'email' => null,
            'first_name' => null,
            'created' => null,
            'last_login' => null,
            'hash' => null
            );
    }

}

0

: .

, $accountModel AccountModel.

Another piece of evidence pointing to this is that the attempt to create AccountModelis an error in your code, as you define it before defining the class Modelon which it depends. So you still cannot do this.

If you still have problems, send a test file to ideone.com, which reproduces the problem noticeably.

0
source

All Articles