I use a data class to feed templates my data, i want to calculate a unique id from the data in the data class so i can check if the template with that data is allready in cache and then serve that version.
so a function to get an unique id from an array of a class would help me out
something like this works but is rather costly md5(serialize($classdata))
im hopeing there is some function to get the unique id without serializing all data, or at least not to have to in php
Thanks in advance, best, paul
edit: i celebrated to soon, the unique id is only the same in the current instance a restart of the same script makes another id, wich then ofcourse is not in cache testscript used:
<?php
class foo {}
$f = new foo;
print spl_object_hash($f);
ill explain in some more depth
class template_data implements IteratorAggregate, ArrayAccess, Countable {
private $_data;
//some methods for the overloaded classes
//
//the getId function
public function getId() {
return hash('md5',serialize($this->_data));
}
}
$t = new template('file');
$d = new template_data('some data');
$t->addData($d);
$t->display();
now if the data given to the template engine is in cache it uses that version preventing to having to re-parse the template for the dataset
this is a simplistic view of the template_data, it is actually lazy loading and uses memcached dataid's so the data isnt actually fetched till it is used in the template
You could try spl_object_hash()
From the docs
This function returns a unique identifier for the object. This id can be used as a hash key for storing objects or for identifying an object.
Why not look into and overriding the __toString()
method on the object to get and hash the relevant data in the object.
For example
class Object
{
// Some vars
public $name = "Jake";
public $age = 26;
public $dob = "1/1/10"
// the toString method
public function __toString()
{
return md5($this->name . $this->age . $this->dob);
}
}
// Create new object
$object = new Object();
// echo the object, this automatically calls your __toString method
echo $object
In this situation you don't use serialize, which is costly, instead just use __toString()
to generate your own unique id based on variables stored with the object.
PHP does not create unique IDs that persist between executions for objects, this means that you are going about producing the desired behavior correctly. So while there is no good answer for the asked question I can give some suggestions to reduce the cost of producing your IDs.
First you can use json_encode
rather than serialize
. Second, you can store the value, so that multiple calls to the function will not re-serialize the data every time.
The
json_encode
function is not only faster thanserialize
, but it also produces a shorter string as output.http://cw-internetdienste.de/2015/05/04/serialize-vs-json_encode/
class template_data implements IteratorAggregate, ArrayAccess, Countable {
private $_data;
private $_id;
//
//some methods for the overloaded classes
//
//the getId function
public function getId() {
if(empty($this->_id))
$this->_id = hash('md5',json_encode($this->_data));
return $this->_id;
}
}
Lastly; the best solution will probably be to cache the output of the template using the route, or arguments as the basis for the unique cache keys rather than the individual data sets used.