Skip to content

Instantly share code, notes, and snippets.

@jbroadway
Created August 21, 2013 15:40
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 jbroadway/6296092 to your computer and use it in GitHub Desktop.
Save jbroadway/6296092 to your computer and use it in GitHub Desktop.
Class for bitwise type value storage.
$ php usage.php
Is 3 type A? 1
Is 3 type B? 1
Is 3 type C? 0
<?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);
}
}
?>
<?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