获取受保护对象中的字符串 [英] Get string within protected object

查看:76
本文介绍了获取受保护对象中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让该对象内部的字符串"this info"被称为$object,但是数据受到保护,如何访问该存储袋?

I am trying to get the the string "this info" inside this object lets call it $object, but data is protected, how can I access that pocket?

    object(something)#29 (1) {
  ["_data":protected]=>
  array(10) {
    ["Id"]=>
    array(1) {
      [0]=>
      string(8) "this info"
    }
    ["SyncToken"]=>
    array(1) {
      [0]=>
      string(1) "0"
    }
    ["MetaData"]=>
    array(1) {

很明显,$object->_data给我一个错误Cannot access protected property

Obviously $object->_data gives me an errorCannot access protected property

推荐答案

如果您-或该类的编写者-希望其他人有权访问受保护或私有财产,则需要通过类本身.

If you - or the writer of the class - want other people to have access to a protected or private property, you need to provide that via a getter method in the class itself.

所以在课堂上:

public function getData()
{
  return $this->_data;
}

在您的程序中:

$object->getData();

这篇关于获取受保护对象中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆