如何阅读“protected”变量

i get a resultset from a class that manages WSDL Data.

I didn't write the code to the class, i only use it.

I call a function to create an ID with the service and want to work with that ID later in the same script.

My Resultset looks like this:

Array
(
    [0] => SaveResult Object
        (
            [id:protected] => newgeneratedID
            [success:protected] => 1
        )
)

So I tried $response[0]->id to get the ID i need. Now I get a fatal error.

PHP Fatal error:  Cannot access protected property SaveResult::$id

I know it´s a noob question, but I don't get why I can print_r the object but not get the values inside.

You cannot use any of protected data from another space except the same object.

But you can edit the SaveResult object and add getter for id:

public function getId() {
   return $this->id;
}

There ought to be a method you can call in the SaveResult class which lets you access the data, something like:

$response[0]->getId();

See the documentation/source code of the class.

You can read a protected property with the ReflectionProperty interface.

The HandyMan component from the phptoolcase library has a handy method to read inaccessible objects properties.

$value = PtcHandyMan::getProperty( $your_object , 'propertyName');

Static property from a singleton class:

$value = PtcHandyMan::getProperty( 'myCLassName' , 'propertyName');

Very simple and usefull, though it is only adviced in few situations as protected/private properties are shouldn't be used outside of their scope.

You can find the HandyMan class here: http://phptoolcase.com/guides/ptc-hm-guide.html