【问题标题】:Get string within protected object获取受保护对象内的字符串
【发布时间】:2012-03-29 18:01:23
【问题描述】:

我试图在这个对象中获取字符串“this info”,我们称之为$object,但是数据受到保护,我怎样才能访问那个口袋?

    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

【问题讨论】:

  • 如果值受到保护,那么“为什么”是有充分理由的。
  • 好吧,我也遇到了同样的问题 Quickbook api :)

标签: php object


【解决方案1】:

有几种替代方法可以获取不需要修改原始源代码的对象的私有/受保护属性。

选项 1 - Reflection:

维基百科将反射定义为

...计算机程序在运行时检查和修改程序的结构和行为(特别是值、元数据、属性和函数)的能力。 [Reflection (computer_programming)]

在这种情况下,您可能希望使用反射来检查对象的属性并将受保护的属性 _data

设置为可访问

我不建议使用反射,除非您有非常具体的用例可能需要它。这是一个关于如何使用 PHP 反射获取私有/受保护参数的示例:

$reflector = new \ReflectionClass($object);
$classProperty = $reflector->getProperty('_data');
$classProperty->setAccessible(true);
$data = $classProperty->getValue($object);

选项 2 - 子类(仅限受保护的属性):

如果类不是final,可以创建原来的子类。这将使您能够访问受保护的属性。在子类中,您可以编写自己的 getter 方法:

class BaseClass
{
    protected $_data;
    // ...
}

class Subclass extends BaseClass
{
    public function getData()
    {
        return $this->_data
    }
}

希望这会有所帮助。

【讨论】:

  • 反射很棒
【解决方案2】:

如果您 - 或类的编写者 - 希望其他人能够访问受保护或私有属性,则需要通过类本身中的 getter 方法来提供。

所以在课堂上:

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

在你的程序中:

$object->getData();

【讨论】:

    【解决方案3】:

    您可以对私有/受保护的属性使用著名的 getter 和 setter 方法。 例如:

    <?php
    
    class myClass
    {
        protected $helloMessage;
    
        public function getHelloMessage()
        {
            return $this->helloMessage;
        }
    
        public function setHelloMessage( $value )
        {
            //Validations
            $this->helloMessage = $value;
        }
    }
    
    ?>
    

    您好,

    埃斯特法诺。

    【讨论】:

      【解决方案4】:

      要检索受保护的属性,您可以使用 ReflectionProperty 接口。

      phptoolcase 对此任务有一个奇特的方法:

      public static function getProperty( $object , $propertyName )
              {
                  if ( !$object ){ return null; }
                  if ( is_string( $object ) ) // static property
                  {
                      if ( !class_exists( $object ) ){ return null; }
                      $reflection = new \ReflectionProperty( $object , $propertyName );
                      if ( !$reflection ){ return null; }
                      $reflection->setAccessible( true );
                      return $reflection->getValue( );
                  }
                  $class = new \ReflectionClass( $object );
                  if ( !$class ){ return null; }
                  if( !$class->hasProperty( $propertyName ) ) // check if property exists
                  {
                      trigger_error( 'Property "' . 
                          $propertyName . '" not found in class "' . 
                          get_class( $object ) . '"!' , E_USER_WARNING );
                      return null;
                  }
                  $property = $class->getProperty( $propertyName );
                  $property->setAccessible( true );
                  return $property->getValue( $object );
              }
      
      
      $value = PtcHandyMan::getProperty( $your_object , ‘propertyName’);
      
      $value = PtcHandyMan::getProperty( ‘myCLassName’ , ‘propertyName’); // singleton
      

      【讨论】:

        猜你喜欢
        • 2018-03-12
        • 2019-05-21
        • 1970-01-01
        • 2013-12-18
        相关资源
        最近更新 更多