Is there a simple PHP way to minimize the class name in serialization? -
i have group of objects need serialize, class names long, example:
"\namespace1\subnamespace\dataobjecta" "\namespace1\subnamespace\dataobjectb" "\namespace1\subnamespace\dataobjectc" "\namespace1\subnamespace\dataobjectd"
using serialize function on objects, get: "o:41:\"namespace1\subnamespace\dataobjectc\":1:{s:4:"data";s:9:"some data";}"
the serialization string contains full class name bigger data :)
i'm familiar
__sleep()
ans__wakeup()
functions, not useful here.i understand king of lookup table required
my question is: is there simple php way minimize class name in serialization
any suggestion welcome
i have answer, bad answer, , answer addresses question.
good answer
if can take somewhere else entirely: don't want @ all. mention class names longer actual data. if case, overall have no data in serialization. unless have ridiculously long namespaces/class names (in case might want reconsider application structure), imagine serialized strings fit into, instance, mysql text field. point is, if have little bit of data, doubt worth effort muck standard format trim off amounts less kilobyte of data. reasonable database , server be able handle these things without trouble, if have millions , millions of such records. unless kind of low-memory embedded device, curious hear why think need (of course rhetorical: doubt running php on embedded device).
if do try this, you're going add code going have maintain @ after , "what in world going on here?". depend on need, i'm suspicious more introduce problems via code make feature happen, letting serialized data long.
bad answer
i don't think want make changes serialized data. answer 1 of questions directly: no, there no way shorten namespace , still use unserialize()
method of php, except ditching namespaces altogether in application. doubt want that.
your other option manually adjust serialized string yourself. store "modified serialized format" (let's call modserialized
). then, when need unserialize, have reverse modserialized function , can run unserialize()
normally. trouble output of php's serialize
method represents standard , well-established encoding. modifying going inherently error-prone and, definition, go against standard best-practices. can without errors if careful , write lots of code, again, don't think want do. instance imagine finding , replacing \namespace1\subnamespace\dataobjecta
gibberish, because want make sure don't accidentally replace found in string. have remember gibberish put in, , represents, can reverse later. if manage successfully, new! re-invented wheel , have ad-hoc compression algorithm built!
so really, don't want either. or if do, take answer @blackbam gave , compress data normal compression algorithm. less weird.
another option
finally, if don't of above suggestions, there 1 more: ditch php serialize()
together. not fit needs. result, better come solution fit needs try modify well-established standard fit problem. going down route give chimera doesn't work anyone.
what like? depends on problem. instance, establish abbreviations class names in system. when comes time serialize make array contains abbreviated class name, , string representation of objects data needs persisted can rebuilt. find encoding that: json, or php serialize, or other format. then, manually build own unserialize()
method reconstruct object own serialization representation. basically, make own serialize()
, unserialize()
.
Comments
Post a Comment