1 | <?php |
||
7 | trait Bits |
||
8 | { |
||
9 | /* |
||
10 | * Available Bit Operations |
||
11 | */ |
||
12 | protected $BIT_OPERATIONS = ['AND', 'OR', 'XOR', 'NOT']; |
||
13 | |||
14 | /** |
||
15 | * Count set bits in a string. |
||
16 | * |
||
17 | * @param string $key |
||
18 | * |
||
19 | * @return int |
||
20 | */ |
||
21 | public function bitCount(string $key): int |
||
25 | |||
26 | /** |
||
27 | * Perform bitwise operations between strings. |
||
28 | * |
||
29 | * @param string $operation AND, OR, NOT, XOR |
||
30 | * @param string $returnKey Return Key |
||
31 | * @param splat $keys List of keys for input |
||
32 | * |
||
33 | * @return int The size of the string stored in the destination key. |
||
34 | */ |
||
35 | public function bitOp(string $operation, string $returnKey, ...$keys): int |
||
49 | |||
50 | /** |
||
51 | * Sets or clears the bit at offset in the string value stored at key. |
||
52 | * |
||
53 | * @param string $key |
||
54 | * @param int $offset |
||
55 | * @param int $value |
||
56 | * |
||
57 | * @return int 0 or 1, the value of the bit before it was set. |
||
58 | */ |
||
59 | public function setBit(string $key, int $offset, int $value): int |
||
63 | |||
64 | /** |
||
65 | * Returns the bit value at offset in the string value stored at key. |
||
66 | * |
||
67 | * @param string $key |
||
68 | * @param int $offset |
||
69 | * |
||
70 | * @return int |
||
71 | */ |
||
72 | public function getBit(string $key, int $offset): int |
||
76 | |||
77 | public function bitField(): bool |
||
81 | |||
82 | public function bitPos(): bool |
||
86 | } |
||
87 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: