Get string within protected object

后端 未结 4 991
庸人自扰
庸人自扰 2020-12-21 05:45

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?

           


        
相关标签:
4条回答
  • 2020-12-21 06:11

    There are a few alternative ways to get a private/protected properties of an object that doesn't require you to modify the original source code.

    Option 1 - Reflection:

    Wikipedia defines Reflection as

    ... the ability of a computer program to examine and modify the structure and behavior (specifically the values, meta-data, properties and functions) of the program at runtime. [Reflection (computer_programming)]

    In this case you may want to use reflection to examine the properties of the object and set as accessible the protected property _data

    I do not recommend reflection unless you have very specific use cases where it may be required. This is an example on how to get your private/protected parameter using Reflection with PHP:

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

    Option 2 - Subclasses (protected properties only):

    If the class is not final, you can create a subclass of the original. This will give you access to the protected properties. In the subclass you could write your own getter methods:

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

    Hope this helps.

    0 讨论(0)
  • 2020-12-21 06:15

    You can use the famous getters and setters methods to private/protected properties. Ex:

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

    Greetings,

    Estefano.

    0 讨论(0)
  • 2020-12-21 06:15

    To retrieve a protected property you can use the ReflectionProperty interface.

    The phptoolcase has a fancy method for this task:

    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
    
    0 讨论(0)
  • 2020-12-21 06:31

    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.

    So in the class:

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

    And in your program:

    $object->getData();
    
    0 讨论(0)
提交回复
热议问题