-
-
Save morrisonlevi/3b0416086a96a766cbc7 to your computer and use it in GitHub Desktop.
A comparison of algebraic data types and normal interitance
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 | |
enum AuthenticationType { | |
Remote, | |
UserDb($hash), | |
PendingRequest($hash) | |
} | |
match ($AuthenticationType) { | |
case AuthenticationType::Remote { | |
} | |
case AuthenticationType::UserDb($hash) { | |
} | |
case AuthenticationType::PendingRequest($hash) { | |
} | |
} |
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
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