when using the dump utility of symfony, there is a nice reference number that identifies the object such as object: Doctrine\ORM\PersistentCollection {#3491, is there any way that one can get this value(3491) without without using the dump function. That is some_function($object) and then get the same number that symfony dump function would return.
Thanks
I believe it is the spl_object_hash() value.
Related
In codeigniter 4 there is a Db helper or query builder when i print_r($db->getLastQuery()) it returns whole methods and other information or when echo $db->getLastQuery() it returns last query.
How the same function returns diffrent output at same time ??
Probably because getLastQuery returns object which implements magic method __toString, and with echo variable is casted to string, while print_r just prints raw object.
In PHP I'm having a real hard time using serialize/unserialize on a large array of objects (100000+ objects). These objects can be of a lot of different types, but are all descendants from a base class.
Somehow when I use unserialize on the array of objects about 0,001% of the objects are generated wrong! A whole different object is generated instead. This does not happen random, but each time with the same objects. But if I change the order of the array, it happens with different objects, so this looks like a bug to me.
I switched to json_encode/json_decode, but found that this always uses stdClass as the object's class. I solved this by including the classname of each object as a property, and then use this property to construct a the new object, but this solution is not very elegant.
Using var_export with eval works fine but is about 3 times slower than the other methods and uses much more memory.
Now my questions are :
what could cause the bug / wrong objects that are created with
unserialize ?
is there a better way to use json_decode with an array of objects, so that classes are somehow stored within the json
automatically?
is there maybe even an other method to read/write a large array of objects in PHP?
UPDATE
I'm beginning to believe there must be something strange with my array-data, because with msgpack_serialize (php extension, alternative to serialize) I get the same kind of errors (but strangely enough not the same objects are generated wrong!).
UPDATE 2
Found a solution : instead of doing serialize on the entire array, I do it on each object now, first serialize and then base64_encode and then I store each serialized object as a separate line in a text-file. This way I can generate the entire array of objects and then iterate each object using file() with unserialize and base64_decode : no more errors!
With serialize/unserialize functions 2 magic methods are connected.
__sleep()
serialize() checks if your class has a function with the magic name __sleep(). If so, that function is executed prior to any serialization. It can clean up the object and is supposed to return an array with the names of all variables of that object that should be serialized. If the method doesn't return anything then NULL is serialized and E_NOTICE is issued.
With sleep you have better control on serializaction you can pass the variables that can be serialized and clean resources befere seralization.
When unserialize is called then the other function should be mentioned
__wakeup()
The intended use of __wakeup() is to reestablish any database connections that may have been lost during serialization and perform other reinitialization tasks.
About json_encode()
It doesn't have magic methods __wakeup, __sleep so you have less control
It doesn't serialize private properties
Objects are always stored as stdClass
Json_encode is faster than serialize
It's is up to you what you choose but for more advanced classes with db connection etc. I would suggest serialize()
I am using CSV Import Pro to import products into my store using OpenCart.
The other day I was importing products which was going fine. Then after an import the extension keeps giving me this fatal error.
I've tried contacting support for damn near 5 days and I haven't gotten much from them. I really need to get this fixed.
Fatal error: Cannot use object of type stdClass as array in /home/content/71/11151671/html/admin/view/template/tool/csv_import.tpl on line 234
Usually there are tabs at the top that let me navigate to the other pieces of the module.
It would not let me post images
With the CSV Import Pro you want to go to the admin/controller/tool/csv_import.php
Search for this line of code:
$this->data[$key] = json_decode($this->data[$key];
and replace it with:
$this->data[$key] = json_decode($this->data[$key], true);
By adding the true at the end, you are basically telling the script you want your data to be in an array format, rather than an object.
Hope this helps ;)
Peter
Without code sample it's really just guessing, but my first impression is that you're trying to access some members of an object that was processed with json_decode. This will, by default, turn arrays into objects, to be more exact instances of PHP's StdClass. Either try accessing the members using object notation ($obj->member) or use the 2nd optional parameter to json_decode in which case the returned will be an associative array instead of an object.
See PHP doc for json_decode.
If you split a JSON-object, try to add a second parameter to the function json_decode:
$json = json_decode($string, true);
I have a function that requires information to be passed to it. The information is contained within an object. Therefore I must pass that object as one of the function arguments. The object is very large however, and I would like to reduce the overhead involved in making copies every time it is passed. Here is an example of
My function Call:
1 myFunction($myObject1);
and the function:
2 function myFunction($myObject2){
3 //do stuff
4 }
I understand there is more to it in php than just pass-by-reference vs pass-by-value. Correct me if I am wrong, but I believe on line 1 there is only a reference to the object made, but on line 2 the object is copied. To avoid this copy I have replaced ($myObject2) with (&$myObject2). I still refer to the object within the function definition as $myObject2 and everything seems to work. I believe I am now using a reference only and therefore making no copies of the object (which was my goal). Is my thinking correct? If not not why?
In PHP5, "objects" are not values. The value of the variables $myObject1, $myObject2 are object references (i.e. pointers to objects). You cannot get "the object itself"; objects can only be manipulated through these pointers.
Assignment and passing by value only copy values. Since objects are not values, they cannot ever be cloned through assignment, passing, etc. The only way to duplicate an object is to use the clone operator.
Putting & on a variable makes it pass or assign by reference, instead of by value without the &. Passing by reference allows you to modify the variable passed. Since the value of a variable cannot be an object, this has nothing to do with objects.
I'm trying to serialize a Doctrine_query object in Symfony :
var_dump(serialize($this->pager->getQuery()));
The result is :
string(2) "N;"
What am I doing wrong?
You can not serialize every object in PHP. Objects themselves - by implementing the Serializeable interface PHP Manual - can protect themselves from being serialized for example.
They return a NULL value then (or don't return anything which is then NULL in PHP). And that's exactly the contents of your serialized string: a serialized NULL (N;).
And there are even some build-in classes that go even further than that. But it applies as well to user-defined classes and build-in classes: Some of them are not available for serialization.
One example of a built-in class that can not be serialized in PHP is DOMDocument, however it is possible to add the functionality as the following question demonstrates:
How to serialize/save a DOMElement in $_SESSION?.