Wasim A.
Wasim A.

Reputation: 9890

Access PHP Objects with namespace

enter image description here

I am trying to integrate an API and its response is saved in $charge variable. I tried to access this via $charge->_status:protected but fail. How to access those variables?

Upvotes: 0

Views: 141

Answers (4)

hakre
hakre

Reputation: 198115

gIn your case it's just

$charge->getStatus();

The way you ask the question shows you've got a little misunderstanding. The property name is "_status" and it's visibility is protected.

This is why the print_r output has it as "_status:protected" but that is not the property name, but an informative encoding.

Protected visibility means, that you can not access it via the class public interface. The module designer has added the public property method getStatus() (a so called "getter" method) so that it is accessible for reading.

You can find the method definition in the source-code here: https://github.com/CKOTech/checkout-php-library/blob/master/com/checkout/ApiServices/Charges/ResponseModels/Charge.php#L269

Upvotes: 2

Kshitij Soni
Kshitij Soni

Reputation: 394

<?php

$data = array(
    '_status:protected' => 'Authorized'
);
$arr = (object)$data;
// print_r($arr);
$key = '_status:protected';
echo $arr->$key;
?>

I think this will help you..

Upvotes: 0

Nitheesh K P
Nitheesh K P

Reputation: 1110

try this json response is in a variable named $charge, you must use json_decode and then do as like this example:

$decoded = json_decode($charge);
$result = $decoded->result;
$quote = $decoded->info->quote;
var_dump($result, $quote);

Upvotes: 0

Elixir Techne
Elixir Techne

Reputation: 1856

Try as

$charge->_status

Based on the given image it seems that the _status is declared as protected and it won't allow you to access outside the class.

Upvotes: 0

Related Questions