0

Here’s part of a var_dump of a response to a request. We’ll call it $response:

object(NeverBounce\Object\VerificationObject)#4 (1) {
["response":protected]=>
array(9) {
["status"]=>
string(7) "success"
["result"]=>
string(7) "invalid"
...

I know how to access the array values of $response and assign them to variables using:

$var1 = response->[status];
$var2 = response->[result];

Except for one little problem... The :protected appended to response.

How do I refer to status and result when assigning them to $var1 and $var2? The syntax above doesn’t work because of the protected state of the array. I did read in the manual and other posts about protected, private, public but being new at this, it made little sense.

Perhaps I am fantasizing but it seems there must be a simple way to do this.

Please advise. All help and comments appreciated.

41358
  • 31
  • 6
  • Possible duplicate of [How to get protected property of object in PHP](https://stackoverflow.com/questions/20334355/how-to-get-protected-property-of-object-in-php) – splash58 Feb 16 '18 at 07:18

2 Answers2

0

You can try using Reflection which is present in PHP:

Wikipedia definition of Reflection ~

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

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);
Amit Merchant
  • 1,045
  • 6
  • 21
0

You can typecast your object to array and access it like normal array values, something like this

$array =  (array) $yourObject;
$status = $array['response']['status'];
$result = $array['response']['result'];
Faraz Irfan
  • 1,306
  • 3
  • 10
  • 17
  • Thanks for the reply! I took your suggestion and got the array as expected. However, there is an asterisk preceding the property name now. $email_addr_valid = $nb_request->[*response][result]; echo $email_addr_valid; yields an empty screen. Any advice? – 41358 Feb 16 '18 at 16:45
  • Did you try to print it without aesterik like [response][result] ? – Faraz Irfan Feb 16 '18 at 20:47