1

uuid_create requires the parameter to be passed by reference.

uuid_create(&$foo);

The problem is that this will produce error:

Message:    Call-time pass-by-reference has been deprecated

Is the PHP extension uuid-php.x86_64 outdated? It is not "compatible" with the PHP 5. What are the alternatives?

Just wantend to highlight that this is not a duplicate.

$foo    = NULL;

uuid_create($foo);

Will produce:

Type:       Run-time warnings (non-fatal error).
Message:    uuid_create(): uuid_create: parameter wasn't passed by reference
Gajus
  • 69,002
  • 70
  • 275
  • 438
  • No. You just put `$foo` in there, without the `&` in front and that's it. I write a comment here and then search the duplicate, we had this question some days ago already. – hakre Jun 02 '12 at 08:53
  • possible duplicate of [Call-time pass-by-reference has been deprecated;](http://stackoverflow.com/questions/4665782/call-time-pass-by-reference-has-been-deprecated) – hakre Jun 02 '12 at 08:54
  • I ended up using `exec('uuid -v 4')`. – Gajus Jun 02 '12 at 08:54
  • Better take a look here: http://php.net/manual/en/function.uniqid.php – hakre Jun 02 '12 at 08:56
  • 1
    @hakre `uniqid()` is a worthless implementation. It is as random as `time()+rand([..])`. – Gajus Jun 02 '12 at 08:57
  • @Guy: UUIDs are not necessarily truly *random* (even though UUID4 is random, but older ones are pretty predictable). Their purpose is to be *unique*. – ThiefMaster Jun 02 '12 at 08:58
  • Also, take a look at the user-comments on that page. I'm still trying to locate your PHP extension to add more reference. – hakre Jun 02 '12 at 08:59
  • @ThiefMaster Well, I am interested in UUID version 4 specifically. I am aware about the other implementations as described in http://en.wikipedia.org/wiki/Universally_unique_identifier. – Gajus Jun 02 '12 at 09:02

1 Answers1

3

PHP does not have a method uuid_create and it's not mentioned in the docs so if it comes from an extension it's most likely not an official one and probably outdated. The fact that the function expects an out parameter instead of returning the value is already a pretty obvious sign that the function is rather bad.

However, writing PHP code to generate an uuid4 is pretty easy as it uses random values for all its fields, i.e. you do not need to access system-specific things such a the MAC address:

function uuid4() {
    return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
        // 32 bits for "time_low"
        mt_rand(0, 0xffff), mt_rand(0, 0xffff),

        // 16 bits for "time_mid"
        mt_rand(0, 0xffff),

        // 16 bits for "time_hi_and_version",
        // four most significant bits holds version number 4
        mt_rand(0, 0x0fff) | 0x4000,

        // 16 bits, 8 bits for "clk_seq_hi_res",
        // 8 bits for "clk_seq_low",
        // two most significant bits holds zero and one for variant DCE1.1
        mt_rand(0, 0x3fff) | 0x8000,

        // 48 bits for "node"
        mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
    );
}
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • I doubt if this is faster than `exec('uuid -v 4')`. – Gajus Jun 02 '12 at 09:02
  • http://svn.php.net/viewvc/pecl/uuid/trunk/uuid.c?revision=297236&view=markup - but I'm puzzled a bit about the warning TS sees. And the function returns normally. – hakre Jun 02 '12 at 09:11
  • @Guy: I assume it is. No need to create a new process etc. – ThiefMaster Jun 02 '12 at 09:19
  • PHP actually does have a UUID module. It's not part of the official distribution, but it's maintained by the PHP core team and released via PECL: https://pecl.php.net/package/uuid – Will Aug 05 '15 at 23:14