Re: New function: spl_object_id() or spl_object_handle()
| From: | Patrick Schaaf | Date: | Fri, 28 Nov 2014 14:02:57 +0000 |
| Subject: | Re: New function: spl_object_id() or spl_object_handle() | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-79277@lists.php.net to get a copy of this message | ||
On Friday 28 November 2014 14:51:55 Ferenc Kovacs wrote:
>
> I also used spl_object_hash() in the past when traversing/custom
> serializing object structures which can have infinite recursions
between
> objects, but even that could be simply solved by storing the already
> traversed objects in an array (as assigning the objects into another
> variable doesn't have much overhead) and checking with in_array.
Checking with in_array is O(N) while an array keyed on
spl_object_hash() with the values being the objects, is stable wrt. hash
string vs. object lifetime, and is O(1) on checking for key existence.
The performance difference will become apparent when you have more
than a handful of objects there.
BTW, the two or three places where I use spl_object_hash(), in this way,
are exactly your use case: as an "already visited" cache during traversal
of somewhat arbitrary input structure (logging / dumping).
best regards
Patrick