Created
August 21, 2013 15:40
-
-
Save jbroadway/6296092 to your computer and use it in GitHub Desktop.
Class for bitwise type value storage.
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 usage.php | |
Is 3 type A? 1 | |
Is 3 type B? 1 | |
Is 3 type C? 0 |
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 myapp; | |
class Type { | |
const A = 1; | |
const B = 2; | |
const C = 4; | |
public static function is_a ($type) { | |
return (bool) ($type & self::A); | |
} | |
public static function is_b ($type) { | |
return (bool) ($type & self::B); | |
} | |
public static function is_c ($type) { | |
return (bool) ($type & self::C); | |
} | |
} | |
?> |
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 | |
// Usage: | |
use myapp\Type; | |
$type = Type::A + Type::B; // $type = 3 | |
printf ("Is %d type A? %d\n", $type, Type::is_a ($type)); | |
printf ("Is %d type B? %d\n", $type, Type::is_b ($type)); | |
printf ("Is %d type C? %d\n", $type, Type::is_c ($type)); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment