note 88434 modified in function.uniqid by danbrown
| From: | danbrown@php.net | Date: | Fri, 23 Jan 2009 19:17:44 +0000 |
| Subject: | note 88434 modified in function.uniqid by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-149715@lists.php.net to get a copy of this message | ||
Better yet, just use the php5-uuid extension and this class to create 'official' UUIDs at
high speed (On my system 1000 UUIDs are created in 0.0064 seconds)
<?php
Usage:
$uuid=new uuid();
echo $uuid->v1();
class uuid {
/**
* This class enables you to get real uuids using the OSSP library.
* Note you need php-uuid installed.
* On my system 1000 UUIDs are created in 0.0064 seconds.
*
* @author Marius Karthaus
*
*/
protected $uuidobject;
/**
* On long running deamons i've seen a lost resource. This checks the resource and creates it
if needed.
*
*/
protected function create() {
if (! is_resource ( $this->uuidobject )) {
uuid_create ( &$this->uuidobject );
}
}
/**
* Return a type 1 (MAC address and time based) uuid
*
* @return String
*/
public function v1() {
$this->create ();
uuid_make ( $this->uuidobject, UUID_MAKE_V1 );
uuid_export ( $this->uuidobject, UUID_FMT_STR, &$uuidstring );
return trim ( $uuidstring );
}
/**
* Return a type 4 (random) uuid
*
* @return String
*/
public function v4() {
$this->create ();
uuid_make ( $this->uuidobject, UUID_MAKE_V4 );
uuid_export ( $this->uuidobject, UUID_FMT_STR, &$uuidstring );
return trim ( $uuidstring );
}
/**
* Return a type 5 (SHA-1 hash) uuid
*
* @return String
*/
public function v5() {
$this->create ();
uuid_make ( $this->uuidobject, UUID_MAKE_V5 );
uuid_export ( $this->uuidobject, UUID_FMT_STR, &$uuidstring );
return trim ( $uuidstring );
}
}
?>
--was--
Better yet, just use the php5-uuid extension and this class to create 'official' UUIDs at
high speed (On my system 1000 UUIDs are created in 0.0064 seconds)
<?php
Usage:
$uuid=new uuid();
echo $uuid->v1();
class uuid {
/**
* This class enables you to get real uuids using the OSSP library.
* Note you need php-uuid installed.
* On my system 1000 UUIDs are created in 0.0064 seconds.
*
* @author Marius Karthaus
*
*/
protected $uuidobject;
/**
* On long running deamons i've seen a lost resource. This checks the resource and creates it
if needed.
*
*/
protected function create() {
if (! is_resource ( $this->uuidobject )) {
uuid_create ( &$this->uuidobject );
}
}
/**
* Return a type 1 (MAC address and time based) uuid
*
* @return String
*/
public function v1() {
$this->create ();
uuid_make ( $this->uuidobject, UUID_MAKE_V1 );
uuid_export ( $this->uuidobject, UUID_FMT_STR, &$uuidstring );
return trim ( $uuidstring );
}
/**
* Return a type 4 (random) uuid
*
* @return String
*/
public function v4() {
$this->create ();
uuid_make ( $this->uuidobject, UUID_MAKE_V4 );
uuid_export ( $this->uuidobject, UUID_FMT_STR, &$uuidstring );
return trim ( $uuidstring );
}
/**
* Return a type 5 (SHA-1 hash) uuid
*
* @return String
*/
public function v5() {
$this->create ();
uuid_make ( $this->uuidobject, UUID_MAKE_V5 );
uuid_export ( $this->uuidobject, UUID_FMT_STR, &$uuidstring );
return trim ( $uuidstring );
}
}
http://php.net/manual/en/function.uniqid.php