Skip to content

Instantly share code, notes, and snippets.

@morrisonlevi
Created April 21, 2015 02:47
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 morrisonlevi/3b0416086a96a766cbc7 to your computer and use it in GitHub Desktop.
Save morrisonlevi/3b0416086a96a766cbc7 to your computer and use it in GitHub Desktop.
A comparison of algebraic data types and normal interitance
<?php
enum AuthenticationType {
Remote,
UserDb($hash),
PendingRequest($hash)
}
match ($AuthenticationType) {
case AuthenticationType::Remote {
}
case AuthenticationType::UserDb($hash) {
}
case AuthenticationType::PendingRequest($hash) {
}
}
interface AuthenticationType {}
class Remote implements AuthenticationType {}
class UserDb implements AuthenticationType {
public $hash;
function __construct($hash) {
$this->hash = $hash;
}
}
class PendingRequest implements AuthenticationType {
public $hash;
function __construct($hash) {
$this->hash = $hash;
}
}
switch (true) {
case $AuthenticationType instanceof Remote:
break;
case $AuthenticationType instanceof UserDb:
$hash = $AuthenticationType->hash;
break;
class $AuthenticationType instanceof PendingRequest:
$hash = $AuthenticationType->hash;
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment