arrays - Parsing PHP object value

303

I have the following

Imgur\Api\Model\Basic Object
(
    [data:Imgur\Api\Model\Basic:private] => Array
        (
            [id] => 1XgbfFV
            [title] => PIC 2 TITLE
            [description] => PIC 2 DESC
            [datetime] => 1472495069
            [type] => image/jpeg
            [animated] => 
            [width] => 590
            [height] => 1382
            [size] => 35307
            [views] => 0
            [bandwidth] => 0
            [vote] => 
            [favorite] => 
            [nsfw] => 
            [section] => 
            [account_url] => 
            [account_id] => 0
            [in_gallery] => 
            [deletehash] => tZUGIGuV9Bfv6lV
            [name] => PIC 2 NAME
            [link] => http://i.imgur.com/1XgbfFV.jpg
            [is_ad] => 
        )

    [success:Imgur\Api\Model\Basic:private] => 1
    [status:Imgur\Api\Model\Basic:private] => 200
)

I trying get the [id] so I tried something like

$basic = $client->api('image')->upload($imageData);
$data = $basic->id;
print_r($data);

but value is empty. what should I do to get the value of [id] into $data

Thanks!

92

Answer

Solution:

All items in the data object are private. What you should do is in your Imgur class, create a new methodgetData which returns the data object.

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

You can also expand this further by creating a magic get method.

public function __get($property)
{
    if( isset($this->data[$property] ) {
        return $this->data[$property];
    }

    return null;
}

If doing it the magic method way, you can then just run,$basic->id and it should respond with1XgbfFV because of the magic getter.

If this is not a class that you have created, you will need to look at the API from Imgur package to determine if there is an accessor for the data array.

People are also looking for solutions to the problem: Put php file in the background on Linux

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.