Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Different ways of a UUID-implementation
<?php
function myFunction(\UUID $uuid) { echo $uuid->toString(); }
function myOtherFunction(\UUIDInterface $uuid) { echo $uuid->toString(); }
// No way!
myFunction(\Acme\UUID1::v4());
// Would work, but: really?
myFunction(\Acme\UUID1::v4()->getUUIDObject())
// would work
myFunction(\Acme\UUID2::v4());
// Could work
myOtherFunction(\Acme\UUID3::v4());
<?php
namespace Acme;
class UUID1
{
private $uuid;
public static function v4() { return new self(\UUID::v4())
private function __construct(\UUID $uuid) { $this->uuid = $uuid; }
public function toString() { return $this->uuid->toString(); }
public function toBin() { return $this->uuid->toBin(); }
public function toHex() { return $this->uuid->toHex(); }
public function toArray() { return explode('-', $this->uuid->toString()); }
public function getUUIDObject() { return $this->uuid; }
}
<?php
namespace Acme;
class UUID2 extends \UUID
{
public function toArray() { return explode('-', $this->toString()); }
}
<?php
namespace Acme;
class UUID implements \UUIDInterface
{
private $uuid;
public static function v4() { return new self(\UUID::v4())
private function __construct(\UUID $uuid) { $this->uuid = $uuid; }
public function toString() { return $this->uuid->toString(); }
public function toBin() { return $this->uuid->toBin(); }
public function toHex() { return $this->uuid->toHex(); }
public function toArray() { return explode('-', $this->uuid->toString()); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment