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();
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 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
I want to chain my class's methods, such as in this way,
$article = New Article();
$article->getRow()->addImages();
Because sometimes I don't need to add images to the article that I request,
$article->getRow();
This is my code,
class Article
{
protected $connection;
public $total;
public $item;
public function __construct()
{
$this->connection = new Database(DSN,DB_USER,DB_PASS);
$this->connection->connect();
}
public function getRow($options = array())
{
// Prepare the SQL.
$sql = "
SELECT*
FROM article AS p
WHERE p.article_id = 'home'
";
$this->total = $this->connection->countRows($sql,array(
$property->type
));
$this->item = $this->connection->fetchRow($sql,array(
$property->type
));
return $this;
}
public function addImages() {
$this->item['images']['items'] = array(
0 => "image 1",
1 => "image 2"
);
$this->item['images']['total'] = 2;
return $this;
}
}
result for $article->getRow()->addImages(),
Article Object
(
[connection:protected] => Database Object
(
[connection:protected] => PDO Object
(
)
[dsn:protected] => mysql:host=localhost;dbname=xxx
[username:protected] => xxx
[password:protected] => xxx
)
[item] => Array
(
[url] => hello
[title] => world
[images] => Array
(
[items] => Array
(
[0] => image 1
[1] => image 2
)
[total] => 2
)
)
[total] => 1
}
As you can see that [connection:protected] is always in the result, and [total] => 1 for the article as well.
But how can I get the result in like this below straight to the requested/ intended data without doing this $article->getRow()->addImages()->item,
Array
(
[url] => hello
[title] => world
[images] => Array
(
[items] => Array
(
[0] => image 1
[1] => image 2
)
[total] => 2
)
)
Is it possible?
I find that $article->getRow()->addImages()->item is 'ugly' for getting a simple data.
When using $this to use method chaining, you inherently pass the whole object in your return. This is how we can use method chaining, but you seem fully aware of that. What you desire seems a bit odd though:
You want to use method chaining (meaning your return value is $this)
You want to receive only your item property (meaning your return value is not $this)
Those seem mutually exclusive as I seriously think a method can not return two different things.
But
you can have your addImages() function return what you desire. Note that this solution prevents any further method chaining...
public function addImages() {
$this->item['images']['items'] = array(
0 => "image 1",
1 => "image 2"
);
$this->item['images']['total'] = 2;
return $this->item; // HERE you return your item instead
}
I've got this object, and i want change pages.total
JPagination Object
(
[limitstart] => 15
[limit] => 5
[total] => 16
[_viewall] =>
[_errors] => Array
(
)
[pages.total] => 4
[pages.current] => 4
[pages.start] => 1
[pages.stop] => 4
)
how can I achieve it? Thanks
Assuming the property is public
$object->{'pages.total'} = 'value';
I'm not familiar with JPagination, so I can't tell if it's a private or public property you are tryin to change, but normally you can just do
$ob->total = 10;
If it's a private property you need to change it from within the object with
$this->total = 10;
I have a function that builds a collection of user objects from the database:
public static function GetUsersByGroup($instanceID, $groupID)
{
$col = null;
if($groupID != null)
{
$col = UserGroup::GetCollection("User" ,_DB_GET_ALL_INSTANCE_USERGROUP_MEMBERS,array ($instanceID, $groupID));
}
else
{
$col = UserGroup::GetCollection("User" ,_DB_GET_ALL_INSTANCE_NOGROUP_MEMBERS,$instanceID);
}
echo "this is the collection I am going to return: <pre>";
print_r($col);
echo "</pre>";
return $col;
}
The method has some debug output at the bottom, but the point is if I call that method with a null groupid param i.e it runs the second condition, it prints out a nice indication of the collection that I expected to receive, which is great.
However ..
Here is my calling method:
echo "<br> Collection passed through is: </br>";
$collection = UserGroup::GetUsersByGroup($this->GetInstance()->id,$grouplist->GetCurrentCommandParam());
print_r($collection);
$userlist->UpdateCollection($collection);
$userlist->DeSelect();
The intresting thing is the output:
this is the collection I am going to return:
Collection Object
(
[_valueType:protected] => User
[_isBasicType:protected] =>
[_validateFunc:protected] =>
[_collection:protected] => Array
(
[0] => User Object
(
[valid] =>
[validationMessage] =>
[id] => 29
[table:private] => user
[fields:private] => Array
(
[title] => mrs
[fname] => Kirsty
[lname] => Howden
[email] => kirsty2#softyolk.com
[password] => xxxxxxxx
[lastlogin] => 2009-07-05 15:20:13
[instanceID] => 2
[deliveryAddress] =>
[invoiceAddress] =>
[tel] => 01752848484
[isAdmin] => 0
[disabled] => 0
[mustAuthorise] =>
[usergroupID] =>
)
[validationRules:private] => Array
(
)
[_profileStartTime:protected] =>
[_profileTag:protected] =>
)
[1] => User Object
(
[valid] =>
[validationMessage] =>
[id] => 31
[table:private] => user
[fields:private] => Array
(
[title] => master
[fname] => Seb
[lname] => Howden
[email] => seb#antithug.co.uk
[password] => xxxxxxxxx
[lastlogin] => 2009-07-09 02:02:24
[instanceID] => 2
[deliveryAddress] => saltash
[invoiceAddress] => saltash
[tel] => 8908908
[isAdmin] => 0
[disabled] => 0
[mustAuthorise] =>
[usergroupID] =>
)
[validationRules:private] => Array
(
)
[_profileStartTime:protected] =>
[_profileTag:protected] =>
)
)
)
Collection passed through is:
this is the collection I am going to return:
Collection Object
(
[_valueType:protected] => User
[_isBasicType:protected] =>
[_validateFunc:protected] =>
[_collection:protected] => Array
(
)
)
Collection Object ( [_valueType:protected] => User [_isBasicType:protected] => [_validateFunc:protected] => [_collection:protected] => Array ( ) )
The object returned has been modified??
If the GetUsersByGroup method is called with a userGroupID i.e the first case, then output is all as expected.
If i remove the conditional from the method and simply return $col = UserGroup::GetCollection("User" ,_DB_GET_ALL_INSTANCE_NOGROUP_MEMBERS,$instanceID); then all output is as expected.
It seems that the else condition executes correctly, and then is corrupted on return, but this only happens if the else condition is present, remove the else condition, and simply return the result of the method call in the else condition, and all is as expected.
Any idea please?
Thanks
ADDED THE UserGroup::GetCollection Method (this is a deep rabbit hole though, could go on)
protected static function GetCollection($class, $sqlID, $params = null)
{
$dal = DAL::GetInstance(); //not to be confused with the Instance object, this is an instance of DAL
$collection = new Collection($class);
$items = $dal->QueryForAssoc($sqlID,$params);
foreach($items as $item)
{
$itemObject = new $class();
$itemObject->LoadFromList($item);
$collection->add($itemObject);
}
return $collection;
}
To further clarify the follwing works fine ::
public static function GetUsersByGroup($instanceID, $groupID)
{
$col = null;
//if($groupID != null)
//{
//$col = UserGroup::GetCollection("User" ,_DB_GET_ALL_INSTANCE_USREGROUP_MEMBERS,array ($instanceID, $groupID));
//}
//else
//{
$col = UserGroup::GetCollection("User" ,_DB_GET_ALL_INSTANCE_NOGROUP_MEMBERS,$instanceID);
// }
return $col;
}
I only see the issue if the line is in the else block.
The likely problem here lies in your UserGroup::GetCollection function. PHP 5 passes all objects by reference, so if you are doing any sort of modification in this routine based on the way you are retrieving these objects, then this modification will persist after UserGroup::GetCollection has finished.
I would examine carefully the differences between these two function calls and make sure there are no object changes happening in UserGroup::GetCollection.
$col = UserGroup::GetCollection("User" ,_DB_GET_ALL_INSTANCE_USERGROUP_MEMBERS,array ($instanceID, $groupID));
vs.
$col = UserGroup::GetCollection("User" ,_DB_GET_ALL_INSTANCE_NOGROUP_MEMBERS,$instanceID);
Turns out the method is being called twice, the second call is using the other condition, and returning a blank collection (the problem result).
By setting an echo in each condition I could see as they are called, and first the null case is called and then the non null.
The actual error is that I had a stateful list calling the method twice in the same postback. Hard to catch.
Thanks for looking