This directory contains reusable utility functions and classes that work across all php-universe targets.
A collection of commonly used utility functions:
formatBytes()- Format bytes to human-readable formatrandomString()- Generate random stringsisValidJson()- Validate JSON stringssafeJsonDecode()- Safe JSON decoding with error handlingarrayMergeDeep()- Deep merge arraysarrayFlatten()- Flatten nested arraystruncate()- Truncate strings with ellipsisslugify()- Convert strings to URL-friendly slugsbetween()- Check if value is between two numbersclamp()- Clamp values between min and maxpercentage()- Calculate percentagesformatNumber()- Format numbers with thousands separatoruuid()- Generate UUID v4
Usage:
use CommonFunctions;
$size = CommonFunctions::formatBytes(1024 * 1024); // "1 MB"
$slug = CommonFunctions::slugify("Hello World!"); // "hello-world"
$id = CommonFunctions::uuid(); // "550e8400-e29b-41d4-a716-446655440000"A lightweight logging utility:
debug()- Log debug messagesinfo()- Log info messageswarning()- Log warningserror()- Log errorsgetLogs()- Get all logsexport()- Export logs in various formats
Usage:
use Logger;
$logger = new Logger('INFO', true);
$logger->info('Application started', ['version' => '1.0.0']);
$logger->error('Something went wrong', ['code' => 500]);
// Get logs
$allLogs = $logger->getLogs();
$errors = $logger->getLogsByLevel('ERROR');Configuration management with environment variable support:
get()- Get configuration valueset()- Set configuration valuehas()- Check if key existsall()- Get all configurationmerge()- Merge configuration
Usage:
use Config;
$config = new Config([
'app' => [
'name' => 'MyApp',
'version' => '1.0.0',
],
], [
'app.debug' => false,
]);
$name = $config->get('app.name'); // "MyApp"
$debug = $config->get('app.debug'); // false (from defaults)All utilities in this directory are designed to work across all php-universe targets:
- ✅ Native binaries
- ✅ WebAssembly
- ✅ iOS applications
- ✅ Embedded devices
Copy the utility files you need into your project's src/ directory.
Add to your composer.json:
{
"autoload": {
"files": [
"utils/CommonFunctions.php",
"utils/Logger.php",
"utils/Config.php"
]
}
}Add as a git submodule or include via Composer if published as a package.
Feel free to add more utility functions! Keep in mind:
- Functions should be platform-agnostic
- Avoid file system operations unless necessary
- Use pure functions when possible
- Document all functions with PHPDoc
- Include usage examples