PHP:从函数内创建类属性

时间:2016-08-25 20:44:23

标签: php

我想在函数

中创建一个类属性,例如$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;

我该怎么做?

0 个答案:

没有答案