从PHP中的Object获取数组值

时间:2012-07-19 11:06:36

标签: php arrays function

我正试图获得 来自数组对象的['text']值 当我尝试print_r($ item)时,我将其作为输出:

ToDo Object
(
[data:private] => Array
    (
        [id] => 128
        [user_id] => 6785
        [view_stat] => 0
        [position] => 12
        [text] => 3rd try
        [dt_added] => 2012-07-17 04:29:08
        [tick] => 0
        [temp_view] => 6785
        [viewer] => 6785
    )
)

如何在php中获取[text]值?感谢

1 个答案:

答案 0 :(得分:4)

由于这些是私有变量,你不能!

您需要在类中创建一个公共函数,它将返回所需的特定数据:

public function getText () {
  return $this -> text;
}

在课堂外你可以像这样检索它:

$class = new ToDo();
$myText = $class -> getText();