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.
Related
I have my code in PHP which is returning this Array of data:
GoCardlessPro\Core\ListResponse Object
(
[records] => Array
(
[0] => GoCardlessPro\Resources\Mandate Object
(
[model_name:protected] => Mandate
[created_at:protected] => 2017-04-01T16:49:09.642Z
[id:protected] => ID001
[links:protected] => stdClass Object
(
[customer_bank_account] => CB001
[creditor] => CR001
[customer] => CU001
)
[metadata:protected] => stdClass Object
(
)
[next_possible_charge_date:protected] => 2017-04-06
[payments_require_approval:protected] =>
[reference:protected] => RE001
[scheme:protected] => bacs
[status:protected] => active
[data:GoCardlessPro\Resources\BaseResource:private] => stdClass Object
(
[id] => 123
[created_at] => 2017-04-01T16:49:09.642Z
[reference] => RE001
[status] => active
[scheme] => bacs
[next_possible_charge_date] => 2017-04-06
[payments_require_approval] =>
[metadata] => stdClass Object
(
)
[links] => stdClass Object
(
[customer_bank_account] => 001
[creditor] => CR001
[customer] => CU001
)
)
[api_response] =>
)
)
)
I want to be able to read the ID of the first item in therecords array.
This data is contained inside a variable called $GC_Mandate;
I have tried these:
echo $GC_Mandate->records->{0}->id;
echo $GC_Mandate->records->0->id;
echo $GC_Mandate->records->[0]->id;
$GC_Mandate = $GC_Mandate->records;
echo $GC_Mandate->{0}->id;
But none will return the data
To get the first record, the syntax you need is $GC_Mandate->records[ 0 ].
However, that object is a GoCardlessPro\Resources\Mandate object and its member id is protected1, so we'd need to know the interface of GoCardlessPro\Resources\Mandate (its public methods1), to know if we can somehow retrieve the value of id.
My guess would be getId(), so the full syntax would become
$GC_Mandate->records[ 0 ]->getId()
But, that's just a guess. You'd have to look into the documentation/class definition of GoCardlessPro\Resources\Mandate, to be sure if you can retrieve id.
Turns out (provided I'm linking to the correct github repository) you can do:
$GC_Mandate->records[ 0 ]->id
since GoCardlessPro\Resources\Mandate extends GoCardlessPro\Resources\BaseResource, which exposes the protected members through GoCardlessPro\Resources\BaseResource::__get()2.
1. visibility in PHP
2. magic methods in PHP
I can't comment so I guess I'll have to post.
You should try to print_r($GC_Mandate); and see what it gives out and then go from there.
Try $GC_Mandate->records[0]->__get('id')
it will return all data ..for perticulat data put this in foreach loop
print_r($GC_Mandate['records']);
In my ORM code I have an Entity with a field fined like so:
//part of entity class Item:
/** #Column(name="product_id", type="integer") */
private $productId;
I then executed this code:
//3 lines ~straight out of Doctrine configuration to get EntityManager
include 'config/doctrine-config.php';
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$em = EntityManager::create($dbParams, $config);
//my own code to retrieve an entity instance:
$instance = $em->find(Item::class, 2);
print_r($instance);
And this is the output I get (skipping few other similar properties):
Application\Entity\Item Object
(
[id:Application\Entity\Item:private] => 2
[description:Application\Entity\Item:private] => Product Kit
[productId:Application\Entity\Item:private] => -1
)
Note how there are 6 (six) lines above that came out of print_r() function.
And everything was fine, Until
Next, I have changed the $productId column to ManyToOne Relationship on my Item Entity class, like so:
/**
* #ManyToOne(targetEntity="Product", inversedBy="id")
* #JoinColumn(name="product_id", referencedColumnName="id")
*/
private $productId;
I ran the same code.
OUT CAME THE UNIVERSE OF 2,392,600 LINES, WHAT?
Two million, three hundred and ninety two thousand, six hundred lines lines of print_r output.
looking at the print-out I see that DoctrineProxies\__CG__\Application\Entity\Product Object contains 2,392,564 lines printed by print_r
Question:
What is exactly in this object and why is it so big as to take up nearly 300Mb of disk space when printed out?
I cannot help but wonder if such complexity is apt to cause performance issues in every-day code. For example, I am not printing out the contents of the $instance variable in my every-day code, but I surely return the humongousness from a method call. Does that mean it is a 300Mb variable that gets passed from i.e. the $em->find(Item::class, 2); call above?
(Very) Partial Listing
Application\Entity\Item Object
(
[id:Application\Entity\Item:private] => 2
[description:Application\Entity\Item:private] => Product Kit
[ProductId:Application\Entity\Item:private] => DoctrineProxies\__CG__\Application\Entity\Product Object
(
[__initializer__] => Closure Object
(
[static] => Array
(
[entityPersister] => Doctrine\ORM\Persisters\Entity\BasicEntityPersister Object
(
[class:protected] => Doctrine\ORM\Mapping\ClassMetadata Object
(
[name] => Application\Entity\Product
[namespace] => Application\Entity
[rootEntityName] => Application\Entity\Product
[inheritanceType] => 1
[generatorType] => 5
[fieldMappings] => Array
(
[id] => Array
(
[fieldName] => id
[type] => integer
[scale] => 0
[length] =>
[unique] =>
[nullable] =>
[precision] => 0
[columnName] => id
[id] => 1
)
[fieldNames] => Array
(
[id] => id
[description] => description
)
[columnNames] => Array
(
[id] => id
[description] => description
)
[idGenerator] => Doctrine\ORM\Id\AssignedGenerator Object
[reflClass] => ReflectionClass Object
(
[name] => Application\Entity\Product
)
[namingStrategy:protected] => Doctrine\ORM\Mapping\DefaultNamingStrategy Object
[instantiator:Doctrine\ORM\Mapping\ClassMetadataInfo:private] => Doctrine\Instantiator\Instantiator Object
)
[conn:protected] => Doctrine\DBAL\Connection Object
(
[_conn:protected] => Doctrine\DBAL\Driver\PDOConnection Object
(
)
[_config:protected] => Doctrine\ORM\Configuration Object
(
[_attributes:protected] => Array
(
[metadataCacheImpl] => Doctrine\Common\Cache\ArrayCache Object
(
[data:Doctrine\Common\Cache\ArrayCache:private] => Array
(
[dc2_b1e855bc8c5c80316087e39e6c34bc26_[Application\Entity\Item$CLASSMETADATA][1]] => Array
(
[0] => Doctrine\ORM\Mapping\ClassMetadata Object
(
[name] => Application\Entity\Item
[namespace] => Application\Entity
[rootEntityName] => Application\Entity\Item
[customGeneratorDefinition] =>
[customRepositoryClassName] =>
[isMappedSuperclass] =>
[isEmbeddedClass] =>
[parentClasses] => Array
[BAZILLION LINES redacted for brevity]
You can't dump a proxy object without XDebug or similar tools (which limit the dumped object size).
The problem is really, really simple:
Proxy -> references EntityManager -> references UnitOfWork -> contains Proxy
This obviously leads to a recursive data-structure dump, which in turn leads to a mess any time you try to dump it without sensible limits.
DoctrineProxies\__CG__\Application\Entity\Product
is a proxy class... which means that doctrine doesn't actually fetch the entity from the database (for performance) unless it is needed (i.e. calling $product->getName() those proxy Classes are in a recursive loop with eachother and are VERY large as you saw... most of the information there you don't really need unless you are diving deep ... you should never use print_r ... in the new symfony 2.7+ i think there is a function called dump() in debug mode ... if you use that to print the entity it has loop protection and it just shows reference numbers ... you can also use the \Doctrine\Common\Util\Debug::dump() that also will print out a smaller list than 2^234234234 lines ...
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 get the following returned result from a print_r of a variable returned from a $metadataPlugin->metadata->listAction($filter, $pager) function. I am trying to echo out the [xml] piece but can't figure out how to do it.
KalturaMetadataListResponse Object ( [objects] => Array ( [0] => KalturaMetadata Object ( [id] => 1 [partnerId] => 8 [metadataProfileId] => 2 [metadataProfileVersion] => 1 [metadataObjectType] => 1 [objectId] => 0 [version] => 1 [createdAt] => 1353093894 [updatedAt] => 1353093894 [status] => 1 [xml] => 1353049200 ) ) [totalCount] => 1 )
I have tried
echo $result['objects'][0]->xml;
and
echo $result[0]->xml;
with no success.
It looks like it should be
$result->objects[0]->xml
Just remember when looking at print_r or var_dump results that any time you see Object (...) that you will need to use -> to access the property listed for that object.
So in this case, you have a base Object set as $result, which you then need to get the objects property from. That property contains an array. At index 0 of the array, is another Object, which is the Object where your xml property resides.
i am using a zend model which returns me an object in form of $row with all values
but i am not able to get value from this array . is this posible to get values without foreach
this is the array returned
Zend_Db_Table_Row Object
(
[_data:protected] => Array
(
[user_id] => 2
[udid] => 34
[firstname] => a
[lastname] => a
[email] => jusic.sl#gmail.com
[username] => abc
[password] => c91718531fd9f8b89c4e
[created_date] => 2010-02-11
[updated_datetime] => 2012-06-25 12:48:17
[lastlogin_datetime] =>
[group_id] => 2
[status] => Active
)
)
i need to get the user_id,firstname,email from this array
any help will be appreciated .
i have tried like
$forgotpassword = $userModel->forgotpassword ( $post ); // which contains this array
$id = $forgotpassword['_data:protected']['id']; exit; // but doesnt seem to work
You cannot access _data directly. It's protected.
From the ZF Reference Guide on Naming Conventions:
[…] variables that are declared with the "private" or "protected" modifier, the first character of the variable name must be a single underscore.
You can do either do (due to __get/__set)
echo $forgotpassword->user_id;
or (due to ArrayAccess)
echo $forgotpassword['user_id'];
or (if you want an array)
$array = $forgotpassword->toArray();
echo $array['user_id'];
Please see the Reference Guide and the code
http://framework.zend.com/manual/en/zend.db.table.row.html
http://framework.zend.com/svn/framework/standard/trunk/library/Zend/Db/Table/Row/Abstract.php