2015-09-18 59 views
0

我试图发展自己的UniqueUserId发生器, 我需要的ID来为这种格式:生成UniqueUserID与哈希

99fbf2be-d41b-4c1c-e44e-c1fde9dd4738 

这是我的代码:

<?php 

namespace OKO\OKOUniqueUserId; 

use Rhumsaa\Uuid\Uuid; 
use Rhumsaa\Uuid\Exception\UnsatisfiedDependencyException; 

class UniqueUserId 
{ 
    /** 
    * @var 
    */ 
    private $uuid; 

    /** 
    * @return Uuid 
    */ 
    public function generate() 
    { 

     try { 
      $this->uuid = Uuid::uuid4(); 
     } catch (UnsatisfiedDependencyException $e) { 
      echo 'Error Generating User Unique ID'; 
     } 

     return $this->uuid; 
    } 
} 

MY OUTPYT现在这个样子:

object(Rhumsaa\Uuid\Uuid)#356 (1) { ["fields":protected]=> array(6) { ["time_low"]=> string(8) "d5a6efdf" ["time_mid"]=> string(4) "3f09" ["time_hi_and_version"]=> string(4) "416a" ["clock_seq_hi_and_reserved"]=> string(2) "b6" ["clock_seq_low"]=> string(2) "e8" ["node"]=> string(12) "7befe7820cb7" } } 

但我还是会想到这一点:

25769c6c-d34d-4bfe-ba98-e0ee856f3e7a 

THX

+2

为什么不按照[RFC 4122](https://www.ietf。)生成适当的UUID(https://github.com/ramsey/uuid)。组织/ RFC/rfc4122.txt) –

+0

喜欢哪一个..>? – John

+0

确定通过作曲家安装UUID库后,它安装在我的Symfony2项目的Vendor文件夹中如何在我的php类中使用它...? – John

回答

1

您会收到一个UUID对象从方法调用回来,只需将它转换为字符串调用对象__toString方法:

$stringUuid = (string)$uuid; 

或者有直接的方法,如果您宁愿:

$stringUuid = $uuid->toString();