Re: New function: spl_object_id() or spl_object_handle()

From: Date: Fri, 28 Nov 2014 13:14:22 +0000
Subject: Re: New function: spl_object_id() or spl_object_handle()
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-79275@lists.php.net to get a copy of this message
2014-11-28 2:13 GMT+01:00 Bostjan Skufca <bostjan@a2o.si>: > Hello everyone, > > this is a proposal to add new function to PHP core: spl_object_id() > > > The story: > ======== > Recently I was debugging some larger libraries and sorely missed a function > that would return an object ID. A function called spl_object_hash() exists, > but it returns identical hashes for equal objects. It returns unique IDs for existing objects. A hash is only reused only when the corresponding object was removed by the GC earlier. So actually asking me it makes more sense to "fix" the behaviour of spl_object_hash(), > You need object ID to be > really sure if two references are indeed the same object or two identical > objects. > > Most of the meat is described in this StackOverflow thread: > > > http://stackoverflow.com/questions/2872366/get-instance-id-of-an-object-in-php > > The OP proposes OB+var_dump() magic, which works if objects are small. > Unfortunatelly I was hitting "Allowed memory exceeded". > > The second answer, given by Gustavo Lopes, describes how to create an > extension for this, which provides new function, named spl_object_id(). I > believe this function should be included in PHP core. > > > > Sample implementation (not tested): > ============================= > /* {{{ proto string spl_object_id(object obj) > Return id for given object */ > PHP_FUNCTION(spl_object_id) > { > zval *obj; > > if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "o", &obj) == FAILURE) > { > return; > } > RETURN_LONG(Z_OBJ_HANDLE_P(obj)); > } > /* }}} */ > > > > Two questions: > ============ > 1. Do you think this function should be included in PHP core? > 2. If so, what should this function be called? I am undecided between > spl_object_id() and spl_object_handle() and corresponding get_...() > variants. > > Ad 2. I lean towards ..._id() as ..._handle() suggests that you can do > something with that handle (think "open file handle", etc). > > > What is your opinion about this? > > Tnx, > b. > -- github.com/KingCrunch

« previous php.internals (#79275) next »