PHP-Infusion is a native extension that adds a collection of bit, numeric, string, array, and utility functions to PHP 5. It grew out of recurring application needs rather than one narrow abstraction: the extension combines bit-field helpers, numeric clamps, byte-string operations, token replacement, duration decomposition, and limited introspection behind one shared module.
The source code is available from the PHP-Infusion repository. Version 1.1 uses the PHP 5 Zend extension API and can be built for compatible PHP environments. Building it for another PHP generation requires the corresponding API adaptations in the extension source.
The Actual Exported API
The extension registers 29 functions. This list follows infusion_functions[] in the C source rather than the loose infusion.php prototype file. In particular, rotbit() and between() are not exported by version 1.1.
Bit operations
| Function | Behavior in version 1.1 |
|---|---|
isbit(int $mask, int $position): bool | Tests whether a zero-based bit position is set. |
setbit(int $mask, int $position, bool $enabled = true): int | Sets or clears one bit. |
invbit(int $mask, int $position): int | Toggles one bit with XOR. |
numbit(int $mask): int | Counts set bits. |
msbit(int $mask): int | Returns the zero-based index of the highest set bit; for 5, the implementation returns 2. |
getint(int $mask, int $first, int $last): int | Extracts the inclusive bit range from $first through $last. |
setint(int $mask, int $first, int $last, int $value): int | Replaces a bit range, but does not mask $value to the requested width. |
These functions assume the integer widths and shift behavior of the original build environment. Fixed masks in msbit(), 1 << $position-style shifts, and the compiler-specific population-count path need a deliberate width and bounds policy in any port.
Numeric operations
| Function | Behavior in version 1.1 |
|---|---|
limit(mixed $value, mixed $maximum): float | Returns the smaller value after numeric coercion; the integer branch also returns a double. |
bound(mixed $value, mixed $minimum, mixed $maximum): int|float | Clamps a number to a closed interval. |
sgn(mixed $value): int | Returns -1, 0, or 1. |
gpp(int $value): int | Returns the largest positive k found such that k * k divides the value and is strictly smaller than it. |
sigfig(float $value, int $figures): float|false | Rounds to 0 through 10 significant figures; zero and negative inputs are not handled robustly. |
checksum(int $value): int | Adds the decimal digits of a positive integer. |
bround(int $value, int $base): int|false | Advances to the next multiple; an exact multiple advances by another full base. |
xround(int $value): int | Rounds upward through a fixed table of powers of ten capped at 1000000000. |
String operations
| Function | Behavior in version 1.1 |
|---|---|
strcut(string $value, int $length): string|null | Returns at most the first $length bytes. |
stroff(string $value, int $offset): string|null | Returns the bytes beginning at a non-negative offset. |
truncate(string $value, int $length, string $suffix = '...'): string | Cuts to $length bytes and then appends the suffix, so the result may be longer than $length. |
isuc(string $value, int $position = 0): bool | Tests one byte against ASCII A through Z. |
islc(string $value, int $position = 0): bool | Tests one byte against ASCII a through z. |
xsprintf(string $format, callable $callback, string $delimiter): string | Passes delimiter-prefixed tokens to a callback and concatenates its results. |
strcal(string $format, string $value, int $length = -1): bool | Checks every byte against a compact character set with ranges such as a-z; it is not a regular-expression engine. |
strical(string $format, string $value, int $length = -1): bool | Performs the same check after lowercasing each input byte. |
strmap(string $template, array $values): string | Replaces numeric or identifier tokens such as {0} and {name}. |
The string functions operate on bytes and use ASCII case tests. They do not provide Unicode-aware slicing, casing, grapheme handling, escaping, or output-context safety. In particular, strmap() is substitution rather than an HTML or SQL security boundary.
Collections, time, and introspection
| Function | Behavior in version 1.1 |
|---|---|
number_chop(int $value, array $parts): array|null | Greedily decomposes a positive integer after sorting the parts in descending order. |
time_chop(int $seconds, mixed $format = 2, bool $asArray = false): string|array | Splits a duration into years through seconds or emits tokens for strmap(). |
kimplode(array $values): stringkimplode(string $glue, array $values): string | Joins array keys, not values; the C implementation also accepts the two arguments in reverse order. |
typeof(mixed $value): int | Exposes the PHP 5 Zend type tag, or -1 for an unsupported type. |
is_ref(mixed &$value): bool | Infers reference status from a PHP 5 zval reference count. |
time_chop() stores intermediate unit counts in a static array and does not clear every slot before the next call. Repeated calls in one process can therefore reuse stale values. The introspection helpers depend directly on PHP 5 internals and should not define a modern public contract.
Building the Extension
The extension uses the standard PHP 5 phpize layout. Its actual configuration switch is --enable-infusion:
git clone https://github.com/infusion/PHP-Infusion.git
cd PHP-Infusion
phpize
./configure --enable-infusion --with-php-config="$(command -v php-config)"
make
make test
sudo make install config.m4 declares the --enable-infusion switch. Select a PHP development environment compatible with the extension API or port the Zend API calls before building it for another PHP generation.
Porting the Extension
A serious PHP 8 port must replace the removed thread-safety macros, update strings and smart_str, modernize HashTable iteration, add complete arginfo and return types, define integer-width and invalid-shift behavior, and replace zval reference-count assumptions. The existing edge cases should be turned into tests before changing semantics.
The function table and behavior notes above define the public surface to preserve during a port. Existing users can move individual helpers or the complete module depending on their deployment model; in either case, the documented byte-string, integer-width, coercion, and reference semantics should be covered by tests before changing them.
References
- [Source]PHP-Infusion source repository, version 1.1.
- [phpize]PHP Manual: Compiling shared extensions with phpize.
- [PHP API]PHP Manual: PHP at the Core.