[PHP] Unique Object Instance ID..?

2009-01-10 Thread Nathan Rixham

Evening All,

Not too often I ask a question here, but here goes;

I'm making an Object class which all of my other classes extend, and I 
need each instance to have it's own unique id, seemed simple but it's 
harder than I thought (the difficulty is when it comes to deciding the 
syntax).


so far each object has an OBJECT_ID which is a simple static incremented 
integer representing the instance of the object, here's a simplified 
example:


abstract class Object {

  private static $INSTANCES = 0;
  private $OBJECT_ID = 0;

  public function __construct()
  {
$this-OBJECT_ID = ++self::$INSTANCES;
  }

}

Now back to the problem, I also need the object to have
private $UNIQUE_ID;

this unique_id needs to have:
timestamp
classname
object_id

combining the three makes a unique id; but it's only unique to that 
session, no good for a real world application as there could be 50 
sessions all running at the same time on the server; and odds are rather 
high that two instances of the object could have the same id. Thus an 
extra completely unique identified needs added to the three values above.


I'd thought a simple random integer would possibly be enough (combined 
with the timestamp), but there could still be collission.


so the best I've got so far is:
 [OBJECT_ID:private] = 3
 [UNIQUE_ID:private] = instance://1231629...@testobject.3:97739
timestamp @ class . instance : randomInteger

all I need is a completely unique id for each object instance that can 
never be repeated at any time, even in a multiserver environment (and 
without using any kind of incremented value from a db table or third 
party app)


thoughts, ideas, recommendations?

regards!

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Unique Object Instance ID..?

2009-01-10 Thread Eric Butera
On Sat, Jan 10, 2009 at 6:31 PM, Nathan Rixham nrix...@gmail.com wrote:
 Evening All,

 Not too often I ask a question here, but here goes;

 I'm making an Object class which all of my other classes extend, and I
 need each instance to have it's own unique id, seemed simple but it's harder
 than I thought (the difficulty is when it comes to deciding the syntax).

 so far each object has an OBJECT_ID which is a simple static incremented
 integer representing the instance of the object, here's a simplified
 example:

 abstract class Object {

  private static $INSTANCES = 0;
  private $OBJECT_ID = 0;

  public function __construct()
  {
$this-OBJECT_ID = ++self::$INSTANCES;
  }

 }

 Now back to the problem, I also need the object to have
 private $UNIQUE_ID;

 this unique_id needs to have:
 timestamp
 classname
 object_id

 combining the three makes a unique id; but it's only unique to that session,
 no good for a real world application as there could be 50 sessions all
 running at the same time on the server; and odds are rather high that two
 instances of the object could have the same id. Thus an extra completely
 unique identified needs added to the three values above.

 I'd thought a simple random integer would possibly be enough (combined with
 the timestamp), but there could still be collission.

 so the best I've got so far is:
  [OBJECT_ID:private] = 3
  [UNIQUE_ID:private] = instance://1231629...@testobject.3:97739
 timestamp @ class . instance : randomInteger

 all I need is a completely unique id for each object instance that can never
 be repeated at any time, even in a multiserver environment (and without
 using any kind of incremented value from a db table or third party app)

 thoughts, ideas, recommendations?

 regards!

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php



I was going to say uuid, but maybe SplObjectStorage might be of some help.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Unique Object Instance ID..?

2009-01-10 Thread Phpster

Unique()?

Bastien

Sent from my iPod

On Jan 10, 2009, at 6:31 PM, Nathan Rixham nrix...@gmail.com wrote:


Evening All,

Not too often I ask a question here, but here goes;

I'm making an Object class which all of my other classes extend,  
and I need each instance to have it's own unique id, seemed simple  
but it's harder than I thought (the difficulty is when it comes to  
deciding the syntax).


so far each object has an OBJECT_ID which is a simple static  
incremented integer representing the instance of the object, here's  
a simplified example:


abstract class Object {

 private static $INSTANCES = 0;
 private $OBJECT_ID = 0;

 public function __construct()
 {
   $this-OBJECT_ID = ++self::$INSTANCES;
 }

}

Now back to the problem, I also need the object to have
private $UNIQUE_ID;

this unique_id needs to have:
timestamp
classname
object_id

combining the three makes a unique id; but it's only unique to that  
session, no good for a real world application as there could be 50  
sessions all running at the same time on the server; and odds are  
rather high that two instances of the object could have the same id.  
Thus an extra completely unique identified needs added to the three  
values above.


I'd thought a simple random integer would possibly be enough  
(combined with the timestamp), but there could still be collission.


so the best I've got so far is:
[OBJECT_ID:private] = 3
[UNIQUE_ID:private] = instance://1231629...@testobject.3:97739
timestamp @ class . instance : randomInteger

all I need is a completely unique id for each object instance that  
can never be repeated at any time, even in a multiserver environment  
(and without using any kind of incremented value from a db table or  
third party app)


thoughts, ideas, recommendations?

regards!

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Unique Object Instance ID..?

2009-01-10 Thread Nathan Nobbe
On Sat, Jan 10, 2009 at 4:31 PM, Nathan Rixham nrix...@gmail.com wrote:

 Evening All,

 Not too often I ask a question here, but here goes;

 I'm making an Object class which all of my other classes extend, and I
 need each instance to have it's own unique id, seemed simple but it's harder
 than I thought (the difficulty is when it comes to deciding the syntax).


checkout spl_object_hash()

http://php.net/function.spl_object_hash

-nathan


Re: [PHP] Unique Object Instance ID..?

2009-01-10 Thread Ross McKay
On Sat, 10 Jan 2009 18:42:01 -0700, Nathan Nobbe wrote:

checkout spl_object_hash()

http://php.net/function.spl_object_hash

NB: only guarantees uniqueness for objects instantiated and in memory at
the same time. See the comments on that page for an example of creating
identical object hashes for two separate and distinct instances.
-- 
Ross McKay, Toronto, NSW Australia
Let the laddie play wi the knife - he'll learn
- The Wee Book of Calvin

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php