MrSmile
MrSmile

Reputation: 1233

Cannot get property from object $this variable

I really confused with the situation. I want to get data from function in my class, that created by ActiveRecord Model. Here a class:

class Bag extends ActiveRecord\Model {
    static $table_name = "bags";
    static $primary_key = 'bag_id';

    public function get_pocket_types() {
        $arr = json_decode($this->pocket_types);
        return $arr;
    }
}

I call it in my main code:

$bag = Bag::find_by_bag_id((int)$_GET['id']);
$types = $bag->get_pocket_types();

It seems to be good, but I have an error Notice: Undefined property: Bag::$pocket_types in models/Bag.php on line 21 when I try to get $this->pocket_types. This field is absolutely exists, like a field bag_id.

I've even tried to debug it (in function get_pocket_types() ):

echo $this->bag_id;
// OK

echo $this->bag_id_NOT_EXISTS;
// Fatal error: Uncaught exception 'ActiveRecord\UndefinedPropertyException' with message 'Undefined property: Bag->bag_id_NOT_EXISTS
// This is just for catch an error of REALLY not existed field

echo $this->pocket_types;
// Notice: Undefined property: Bag::$pocket_types in models/Bag.php on line 21

I called var_dump($this); in the function:

object(Bag)#16 (6) { ["errors"]=> NULL
["attributes":"ActiveRecord\Model":private]=> array(2) { ["bag_id"]=> int(160) ["pocket_types"]=> string(0) "" } ["__dirty":"ActiveRecord\Model":private]=> array(0) { } ["__readonly":"ActiveRecord\Model":private]=>
bool(false) ["__relationships":"ActiveRecord\Model":private]=>
array(0) { } ["__new_record":"ActiveRecord\Model":private]=>
bool(false) }

Somebody can explain what happens please?

Upvotes: 2

Views: 508

Answers (1)

Lee Kowalkowski
Lee Kowalkowski

Reputation: 11751

Looks like you have created a custom getter with the same name as an attribute.

In which case you will need $this->read_attribute('pocket_types') instead of $this->pocket_types

Or, rename get_pocket_types to something like get_json_decoded_pocket_types.

Upvotes: 2

Related Questions