Page 1 of 1

Access to class property as an array [SOLVED]

Posted: Mon Jan 08, 2007 11:02 am
by matthijs
I have this class:

Code: Select all

<?php

class myclass {

    function myclass($foo, $bar, $baz) {
        $this->foo    = $foo;
        $this->fields = array('id' => $bar, 'name' => $baz);
    }
    function test($id){
        echo $id;
        $thisnode = $this->fields->id;  // this returns an error
        echo $thisnode;
    }
}

$test = new myclass('foo','bar', 'baz');
$test->test(3);
output:

Code: Select all

3
Notice: Trying to get property of non-object in /Applications/MAMP/htdocs4/testclass.php on line 14
I'm trying to access one of the elements of the array $this-fields, ($id=>$bar)

So I was hoping to get the result: '3bar'

I have tried any variation, but I'm lost. Any ideas?

Posted: Mon Jan 08, 2007 11:11 am
by feyd
$this->fields isn't an object.

Posted: Mon Jan 08, 2007 11:16 am
by matthijs
Ok, I feel very stupid. This works:

Code: Select all

$thisnode = $this->fields['id'];
Sorry.. time to take a break.