Access to class property as an array [SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Access to class property as an array [SOLVED]

Post 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?
Last edited by matthijs on Mon Jan 08, 2007 11:17 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$this->fields isn't an object.
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

Ok, I feel very stupid. This works:

Code: Select all

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