John
John

Reputation: 1665

Generating UniqueUserID with hashes

I am trying to develop my own UniqueUserId generator, I need the ID to to be in this format:

99fbf2be-d41b-4c1c-e44e-c1fde9dd4738

This is my code:

    <?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 LIKE THIS NOW:

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" } }

but I still would expect this:

25769c6c-d34d-4bfe-ba98-e0ee856f3e7a

thx

Upvotes: 0

Views: 50

Answers (1)

Dan Belden
Dan Belden

Reputation: 1217

You receive a UUID object back from your method call, simply cast it to a string to invoke the objects __toString method:

$stringUuid = (string)$uuid;

Or there is a direct method if you would prefer that:

$stringUuid = $uuid->toString();

Upvotes: 1

Related Questions