PHP – Abstracting uniqid()’s more_entropy parameter

This is more of an observation/thoughts post and not a tutorial or analysis, so apologies if it is not in the same style you have grown accustomed to on here. 🙂

The other day while working with some PHP, I looked up the documentation for the uniqid() function to discover what it did and why this code was using it. At the bottom of the page is the following note:

Note: Under Cygwin, the more_entropy must be set to TRUE for this function to work.

What struck me about this note was the lack of any other environments the more_entropy parameter must be set on for the function to work. It would appear it is only required for Cygwin.

This got me thinking: why not let PHP abstract the parameter requirement under Cygwin? In programming, we use OOP abstraction to hide the processes from the outside and instead provide APIs to interact with the object, sometimes known as a black-box. This whole requirement feels like a poor API design to me. I mean, if the parameter is truly required on one or two environments among the many PHP-supported platforms for the function to work, why not internally set more_entropy on those environments and make the API “just work” and not make the developer think about it? The following PHP demonstrates how easy this would be to implement.

function uniqid($prefix='', $more_entropy=false) {
  // Set the more_entropy param if we are
  // in an environment that requires it
  $paramEnvReq = array('cygwin');

  if (in_array(stripos(php_uname('s')), $paramEnvReq)) {
    $more_entropy = true;

To me, abstracting such a parameter makes much more sense than leaving the unchangeable requirement up to the developer, especially since it is only required on one single environment.

I doubt any core PHP developer will see this blog post (if you are a core dev and happen to be reading this though, please comment and let me know!) and maybe consider proposing an RFC consisting of this blog post (but if that happens, please let me know that too!), but I decided to share this anyway. Maybe it makes sense, maybe it does not, who knows.

I am trying to get a “normal” post ready for publishing for you. It is just that I have so many ideas to write on but not enough time to do it plus all the other day activities! 😛



One thought on “PHP – Abstracting uniqid()’s more_entropy parameter

Comments are closed.