Skip to content

Instantly share code, notes, and snippets.

@heiglandreas
Last active September 5, 2017 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heiglandreas/452dae591d071cbdfb78b431cb6597fa to your computer and use it in GitHub Desktop.
Save heiglandreas/452dae591d071cbdfb78b431cb6597fa to your computer and use it in GitHub Desktop.
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