I am trying to access the following and need to get the value of [vid] array cell.
FieldCollectionItemEntity Object
(
[fieldInfo:protected] =>
[hostEntity:protected] => stdClass Object
(
**[vid]** => 119
[uid] => 1
[title] => My Page Name
[log] =>
[status] => 1
[comment] => 1
[promote] => 0
[sticky] => 0
[vuuid] => 3304d1cf-e3cf-4c5a-884a-4abb565ddced
[nid] => 119
[type] => subpage
[language] => und
[created] => 1408621327
[changed] => 1408640191
[tnid] => 0
[translate] => 0
[uuid] => 39145013-6637-4062-96e7-1b4589609c4f
[revision_timestamp] => 1408640191
I tried the following, but I guess I don't have a clue from here:-
$mything = new myClass;
print $mything->accessObjectArray();
class myClass {
protected $var;
function accessObjectArray(){
return $this-> $var;
}
//other member functions
}
Update
I actually only have access to the variable $content which has the following multi-dimensional arrays. All I want is to get the array cell's value of [vid].
To do that, I could print $content["field_image_title"]["#object"] but after that it's protected. That's where I am wondering that how can I access this array. I unfortunately do not have access FieldCollectionItemEntity to include in my page.
On doing this:- I get the following output:-
print_r($content);
Array
(
[field_image_title] => Array
(
[#theme] => field
[#weight] => 0
[#title] => Image Title
[#access] => 1
[#label_display] => hidden
[#view_mode] => full
[#language] => und
[#field_name] => field_image_title
[#field_type] => text
[#field_translatable] => 0
[#entity_type] => field_collection_item
[#bundle] => field_image_collection
[#object] => FieldCollectionItemEntity Object
(
[fieldInfo:protected] =>
[hostEntity:protected] => stdClass Object
(
[vid] => 119
[uid] => 1
[title] => My Page Name
[log] =>
[status] => 1
[comment] => 1
[promote] => 0
[sticky] => 0
[vuuid] => 3304d1cf-e3cf-4c5a-884a-4abb565ddced
[nid] => 119
[type] => subpage
[language] => und
[created] => 1408621327
[changed] => 1408640191
[tnid] => 0
[translate] => 0
[uuid] => 39145013-6637-4062-96e7-1b4589609c4f
[revision_timestamp] => 1408640191
[revision_uid] => 1
"$this-> $var;" this mean variable variable, and this throw php notice undefined variable $var,
you have to use
return $this->var;
or
return $this->vid
what your are doing with this:
return $this-> $var;
is accessing a property named after what is contained in your $var variable which does not contain anything in the scope where it is defined. pass it as a function argument:
function accessObjectArray($var){
return $this-> $var;
}
print $mything->accessObjectArray('vid');
but in any event, that won't work either since (as mentioned by #MikeBrant) you have an object in your parent object properties. something like this might work better
$o = new FieldCollectionItemEntity() // assumes this will construct the object in the state you have posted it
$o->accessObjectArray('hostEntity')->accessObjectArray('vid');
note that the method accessObjectArray($var) must be defined in both objects for this to work
the idea of a protected property is to prevent what you want to actually happen. But! protected means that only the class and it's extending classes can access a value. Make your own class that extends the other one:
class myClass extends FieldCollectionItemEntity {
function accessParentProtectedVars($var){
return $this->hostEntity->$var;
}
//other member functions
}
then your accessObjectArray() function will be able to acces the protected property. note that it's hardcoded to access the hostEntity object.
but seriously, you may want to consult the creator of the other class and maybe you will devise a way to best manage this. My proposed solution is not that much of a good practice if I daresay.
Answer for Drupal members as rendered array in the question looks like Drupal array
I believe you don't need a new class at all, you only need to get node's objects. So, below one line will work for you.
$parent_node = menu_get_object();
Now, you can access by $parent_node->vid
Related
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!
If this is the same code you're using in PHP to talk to the Imgur API then as you can see from the code it has a getData() public method in /lib/Imgur/Api/Model/Basic.php on line 60, which returns the private member. Because the member is private you can't access it directly from your $basic object, which appears to be an instance of Imgur\Api\Model\Basic.
So instead you use the public getter like this...
$basic = $client->api('image')->upload($imageData);
$data = $basic->getData();
var_dump($data->id);
All items in the data object are private. What you should do is in your Imgur class, create a new method getData 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 with 1XgbfFV 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.
I am using the pagination using zend framework2 and i am getting the array as below
Zend\Paginator\Paginator Object
(
[cacheEnabled:protected] => 1
[adapter:protected] => Zend\Paginator\Adapter\ArrayAdapter Object
(
[array:protected] => Array
(
[Id] => 123
[AccountId] => 1
[Name] => abc abc
[AccountName] => a1
)
[count:protected] => 4
)
[currentItemCount:protected] =>
[currentItems:protected] =>
[currentPageNumber:protected] => 1
[filter:protected] =>
[itemCountPerPage:protected] => 25
[pageCount:protected] => 1
[pageRange:protected] => 10
[pages:protected] =>
[view:protected] =>
)
But my question is how can i access the Id,AccountId,Name,AccountName individual without using the foreach loop ?
As mentioned in documentation, there is public function getItem
public function getItem($itemNumber, $pageNumber = null)
Using it is not that simple as accessing by index of array, but keep in mind that internal data in adapter is usually lazy loaded.
So you'd better to write some wrapper class with ArrayAcccess implemented which relies on getItem in case you want to access elements by index.
I'm trying to get the
['text'] value from an array object.
When I try to print_r($item), I get this as output:
ToDo Object
(
[data:private] => Array
(
[id] => 128
[user_id] => 6785
[view_stat] => 0
[position] => 12
[text] => 3rd try
[dt_added] => 2012-07-17 04:29:08
[tick] => 0
[temp_view] => 6785
[viewer] => 6785
)
)
how to get the [text] value in php?? thanks
Since those are private variables, you can't!
You need to create a public function inside the class, that will return the specific data needed:
public function getText () {
return $this -> text;
}
And outside the class you can retrieve it like this:
$class = new ToDo();
$myText = $class -> getText();
I was checking joomla 1.6 index.php and I found the following code at the last line
echo $app;
this prints the entire page contents.
I just printed out the contents in this object using print_r() and I got the following details
JSite Object
(
[template:JSite:private] => stdClass Object
(
[id] => 6
[home] => 1
[template] => beez5
[params] => JRegistry Object
(
[data:protected] => stdClass Object
(
[wrapperSmall] => 53
[wrapperLarge] => 72
[logo] => images/sampledata/fruitshop/fruits.gif
[sitetitle] => Matuna Market
[sitedescription] => Fruit Shop Sample Site
[navposition] => left
[html5] => 0
)
)
)
[_language_filter:JSite:private] =>
[_detect_browser:JSite:private] =>
[_clientId:protected] => 0
[_messageQueue:protected] => Array
(
)
[_name:protected] => site
[scope] =>
[requestTime] => 2011-10-17 17:23
[startTime] => 1318872200.5365
[_errors:protected] => Array
(
)
)
so how echo $app display all the site contents, it doesn't contains any HTML contents in the object.
Thank you very much
It declares the magic method __toString() in the class.
If this function is declared in a class, the return value of it will be used when the object is casted to a string.
Simple example: http://codepad.org/UmZUQA3v
$app is an object, and print_r accesses its values in different ways from echo. When echo is called, it also implicitly calls the magic __toString method. That has been defined such that it returns a string with the page contents, given the values stored inside of the object. print_r will give you those values, but not the __toString representation.
I have an object called $object formed as follows:
stdClass Object ( [PLAYER_ID] => 141 [STATUS_ID] => 16 [LOGIN_NAME] => mikemo21 [EMAIL] => mmogilefsky#yahoo.com [PT_BALANCE] => 13775 )
I'd like to add a parameter to this, perhaps so it appears as follows:
stdClass Object ( [PLAYER_ID] => 141 [STATUS_ID] => 16 [LOGIN_NAME] => mikemo21 [EMAIL] => mmogilefsky#yahoo.com [PT_BALANCE] => 13775 [NEW_FIELD] => VALUE)
What would be the proper syntax to accomplish something like this?
Object properties in PHP do not need to be declared before they are assigned, so you can just assign a new property like you would an existing one:
$object->new_field = $value;
See this in action at http://www.ideone.com/a8Q3t.