我想在函数
中创建一个类属性,例如$user->first_name
class user {
public $first_name = 'Bob';
}
$user = new user;
echo $user->first_name; // OK
现在从函数中执行此操作...
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;
我该怎么做?