Failed to get an object property that containing ":protected" [duplicate] - php

This question already has answers here:
What is the difference between public, private, and protected?
(16 answers)
Closed 9 years ago.
I used the code below to get the object's property _items:protected, but no luck.
$obj = JSite::getMenu();
print_r($obj->_items:protected);
Object output
JMenuSite Object
(
[_items:protected] => Array
(
[101] => stdClass Object
(
[id] => 101
[menutype] => mainmenu
Anyone know what wrong with my code? Thanks.

You are trying to access a protected property from outside the class.
A protected property can only be accessed from within the class itself or classes that inherit from that class.
Read up on OOP access modifiers.

Related

How to Grab stdClass Object Result [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 6 years ago.
can i ask ? . I have class cURL POST into a website ( API Integration ) , and if i post with class. The result is (i use print_r) :
stdClass Object
(
[order] => 200029
)
1
How to grab [order] value?
You can use:
$yourVar->order;

Getting an item with a # from an stdClass [duplicate]

This question already has answers here:
Special characters in property name of object
(2 answers)
Closed 9 years ago.
When I do a print_r($this->info->artist->image[4]);, I get this:
stdClass Object
(
[#text] => http://userserve-ak.last.fm/serve/_/68145708/AWOLNATION+PNG.png
[size] => mega
)
How do I get the [#text] part?
You would do:
$this->info->artist->image[4]->{'#text'};
$this->info->artist->image[4]->{'#text'};

How to check if stdClass object is empty or not in php? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to Check if an Object is empty in PHP
I have this empty object
Array (
[cart_items] => stdClass Object
(
)
)
When I use empty()/is_null() , it doesn't work. When I use sizeof($object), it returns one.
How can I check it?
Cast to an array first
$tmp = (array) $object;
var_dump(empty($tmp));
The reason is, that an object is an object and there is no useful definition of "an empty object", because there are enough classes out there, that only contains methods, but no properties. Should they considered as "empty"?
Check if count( (array)$yourObject) ) == 0.
But I'd better define my own class, and provide it with a meaningful isEmpty() method.

Get a PHP object property that is a number [duplicate]

This question already has answers here:
How to access object properties with names like integers or invalid property names?
(7 answers)
Closed 3 years ago.
I'm trying to get a property from JSON data decoded into a PHP object. It's just a YouTube data API request that returns a video object that has a content object liks so;
[content] => stdClass Object
(
[5] => https://www.youtube.com/v/r4ihwfQipfo?version=3&f=videos&app=youtube_gdata
[1] => rtsp://v4.cache7.c.youtube.com/CiILENy73wIaGQn6pSL0waGIrxMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp
[6] => rtsp://v6.cache3.c.youtube.com/CiILENy73wIaGQn6pSL0waGIrxMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp
)
Doing
$object->content->5
Throws "unexpected T_DNUMBER" - which makes perfect sense. But how do I get the value of a property that is a number?
I'm sure I should know this. Thanks in advance.
This should work:
$object->content->{'5'}
Another possibility is to use the 2nd parameter to json_decode:
$obj = json_decode(str, true);
You get an array instead of a PHP object, which you can then index as usual:
$obj['content'][5]
JSON encode, and then decode your object passing true as the second param in the decode function. This will return an associative array.
$array = json_decode(json_encode($object), true);
Now you can use your new array
echo $array['content']['5'];
Using $object->content->{'5'} will not work if the object was created by casting an array to an object.
A more detailed description can be found here: https://stackoverflow.com/a/10333200/58795

PHP get index of object [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Get Instance ID of an Object in PHP
I'm new to OOP and I have an object. If I:
var_dump($obj);
I get:
object(stdClass)[55]
public 'date' => int 1295297161
public 'id' => int 11
How can I retrieve the "55"?
As others have mentioned you can't read it, without parsing the output of var_dump. But if all you are looking for is a way to uniquely identify an object, then you should use spl_object_hash.

Categories