Last active
September 5, 2017 19:31
-
-
Save heiglandreas/452dae591d071cbdfb78b431cb6597fa to your computer and use it in GitHub Desktop.
Different ways of a UUID-implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme; | |
class UUID2 extends \UUID | |
{ | |
public function toArray() { return explode('-', $this->toString()); } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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