Req #52657 [NEW]: create a spl_object_id function

From: Date: Fri, 20 Aug 2010 15:34:25 +0000
Subject: Req #52657 [NEW]: create a spl_object_id function
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-153658@lists.php.net to get a copy of this message
From:             
Operating system: ANY
PHP version:      Irrelevant
Package:          SPL related
Bug Type:         Feature/Change Request
Bug description:create a spl_object_id function

Description:
------------
the problem with spl_object_hash is, that it is only unqiue for the
existing objects.

For a given Object:
class container {
protected $storage=array();
  public function add(test1 $obj) {
    if(!isset($this->storage[spl_object_hash($obj)])) {
      $this->storage[spl_object_hash($obj)]=$obj;
    }
  }
}

This leads to a problem, that the add method receives the same hash in to
following cases:
CASE1:
$o=new container();
$o->add(new test1("lalala"));
$o->add(new test1("lololo")); // same hash, that's NOT ok!

CASE2:
$t=new test("lalala");
$o=new container();
$o->add($t);
$o->add($t); // same hash, that's ok!

Since there is nothing wrong, with the spl_object_hash() method, i suggest
to introduce a new spl_object_id() function. This could simply return an
(internal) uint32, that is attached to every object on its creation. This
counter gets incremented on every object that gets created. :)


Test script:
---------------
// just with spl_object_hash :/
class container {
protected $storage=array();
  public function add(test1 $obj) {
    if(!isset($this->storage[spl_object_hash($obj)])) {
      $this->storage[spl_object_hash($obj)]=$obj;
    }
  }
}

$o=new container();
$o->add(new test1("lalala")); // will be added
$o->add(new test1("lololo")); // not added - NOT as expected

$t=new test("lalala");
$o=new container();
$o->add($t); // will be added
$o->add($t); // not added - as expected

Expected result:
----------------
// with the new spl_object_id function :)
class container {
protected $storage=array();
  public function add(test1 $obj) {
    if(!isset($this->storage[spl_object_id($obj)])) {
      $this->storage[spl_object_id($obj)]=$obj;
    }
  }
}

$o=new container();
$o->add(new test1("lalala")); // will be added
$o->add(new test1("lololo")); // will be added

$t=new test("lalala");
$o=new container();
$o->add($t); // will be added
$o->add($t); // not added


-- 
Edit bug report at http://bugs.php.net/bug.php?id=52657&edit=1
-- 
Try a snapshot (PHP 5.2):            http://bugs.php.net/fix.php?id=52657&r=trysnapshot52
Try a snapshot (PHP 5.3):            http://bugs.php.net/fix.php?id=52657&r=trysnapshot53
Try a snapshot (trunk):              http://bugs.php.net/fix.php?id=52657&r=trysnapshottrunk
Fixed in SVN:                        http://bugs.php.net/fix.php?id=52657&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=52657&r=needdocs
Fixed in release:                    http://bugs.php.net/fix.php?id=52657&r=alreadyfixed
Need backtrace:                      http://bugs.php.net/fix.php?id=52657&r=needtrace
Need Reproduce Script:               http://bugs.php.net/fix.php?id=52657&r=needscript
Try newer version:                   http://bugs.php.net/fix.php?id=52657&r=oldversion
Not developer issue:                 http://bugs.php.net/fix.php?id=52657&r=support
Expected behavior:                   http://bugs.php.net/fix.php?id=52657&r=notwrong
Not enough info:                     http://bugs.php.net/fix.php?id=52657&r=notenoughinfo
Submitted twice:                     http://bugs.php.net/fix.php?id=52657&r=submittedtwice
register_globals:                    http://bugs.php.net/fix.php?id=52657&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=52657&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=52657&r=dst
IIS Stability:                       http://bugs.php.net/fix.php?id=52657&r=isapi
Install GNU Sed:                     http://bugs.php.net/fix.php?id=52657&r=gnused
Floating point limitations:          http://bugs.php.net/fix.php?id=52657&r=float
No Zend Extensions:                  http://bugs.php.net/fix.php?id=52657&r=nozend
MySQL Configuration Error:           http://bugs.php.net/fix.php?id=52657&r=mysqlcfg



Thread (15 messages)

« previous php.bugs (#153658) next »