0

so i'm having this class:

class c_user
{
    function __construct($userID)
    {
        dbOpen($db); // open database
        $sq = "select * from users where user_id='".$userID."'";
        $rs = getRS($sq, $db); // query database - simplified
        $this->firstname = $rs['firstname'];
    }
}

i'm using it like this:

$user = new c_user(123);
echo $user->firstname;

is it possible accessing data as associative array? like that:

echo $user['firstname'];

thanks

0

2 Answers 2

3

It is, you just have to make your class implement the ArrayAccess interface.

0

How about this?

class c_user 
{
    function __construct($userid)
    {
        $rs = array('firstname'=>'myfirstname');
        $this->firstname = $rs['firstname'];
    }
}

$user = new c_user(123);
//echo $user->firstname;
$user =  (array) $user;
echo $user['firstname'];

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.