Well you can't serialize()
closures straight on, but you can do a workaround, since serialize()
invokes __sleep()
when it's serializing objects, so it gives the object to option to clean things up! This what we're doing here:
class a {
protected $whatever;
function __construct() {
$this->whatever = function() {};
}
public function __sleep() {
$r = [];
foreach ($this as $k => $v){
if (!is_array($v) && !is_string($v) && is_callable($v))
continue;
$r[] = $k;
}
return $r;
}
}
So now you can use serialize()
with md5()
to compare your objects like this:
var_dump(md5(serialize($b)) === md5(serialize($c)));
output:
bool(true)