0

I would like to create a class property such as $user->first_name from within a function

class user {

    public $first_name = 'Bob';

}

$user = new user;

echo $user->first_name; // OK

Now to do this from within a function...

public function get_user($key) {

    $data = array(); // This would be an array returned from a MySQL query

    foreach ($data as $k => $v) {

      if ($k == $key) {

        return $v;

    }

}

$user = new user;

echo $user->get_user('first_name'); 
// Works, but I'd like the same format as above. IE:
// $user->first_name;

How can I do this?

MultiDev
  • 10,389
  • 24
  • 81
  • 148

0 Answers0