raw Software

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

FunctionBehavior in version 1.1
isbit(int $mask, int $position): boolTests whether a zero-based bit position is set.
setbit(int $mask, int $position, bool $enabled = true): intSets or clears one bit.
invbit(int $mask, int $position): intToggles one bit with XOR.
numbit(int $mask): intCounts set bits.
msbit(int $mask): intReturns the zero-based index of the highest set bit; for 5, the implementation returns 2.
getint(int $mask, int $first, int $last): intExtracts the inclusive bit range from $first through $last.
setint(int $mask, int $first, int $last, int $value): intReplaces 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

FunctionBehavior in version 1.1
limit(mixed $value, mixed $maximum): floatReturns the smaller value after numeric coercion; the integer branch also returns a double.
bound(mixed $value, mixed $minimum, mixed $maximum): int|floatClamps a number to a closed interval.
sgn(mixed $value): intReturns -1, 0, or 1.
gpp(int $value): intReturns the largest positive k found such that k * k divides the value and is strictly smaller than it.
sigfig(float $value, int $figures): float|falseRounds to 0 through 10 significant figures; zero and negative inputs are not handled robustly.
checksum(int $value): intAdds the decimal digits of a positive integer.
bround(int $value, int $base): int|falseAdvances to the next multiple; an exact multiple advances by another full base.
xround(int $value): intRounds upward through a fixed table of powers of ten capped at 1000000000.

String operations

FunctionBehavior in version 1.1
strcut(string $value, int $length): string|nullReturns at most the first $length bytes.
stroff(string $value, int $offset): string|nullReturns the bytes beginning at a non-negative offset.
truncate(string $value, int $length, string $suffix = '...'): stringCuts to $length bytes and then appends the suffix, so the result may be longer than $length.
isuc(string $value, int $position = 0): boolTests one byte against ASCII A through Z.
islc(string $value, int $position = 0): boolTests one byte against ASCII a through z.
xsprintf(string $format, callable $callback, string $delimiter): stringPasses delimiter-prefixed tokens to a callback and concatenates its results.
strcal(string $format, string $value, int $length = -1): boolChecks 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): boolPerforms the same check after lowercasing each input byte.
strmap(string $template, array $values): stringReplaces 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

FunctionBehavior in version 1.1
number_chop(int $value, array $parts): array|nullGreedily decomposes a positive integer after sorting the parts in descending order.
time_chop(int $seconds, mixed $format = 2, bool $asArray = false): string|arraySplits a duration into years through seconds or emits tokens for strmap().
kimplode(array $values): string
kimplode(string $glue, array $values): string
Joins array keys, not values; the C implementation also accepts the two arguments in reverse order.
typeof(mixed $value): intExposes the PHP 5 Zend type tag, or -1 for an unsupported type.
is_ref(mixed &$value): boolInfers 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