mordilion/mutils

Useful little helpers

v1.2.3 2023-09-13 15:38 UTC

This package is auto-updated.

Last update: 2024-11-13 18:14:03 UTC


README

MUtils is a collection of useful functions and to provide some PHP 8.0+ features for older PHP versions.

PHP 8.0+ Features

If you use the functions from this Library, it checks if you are using PHP 8.0+ and use then the internal functions (pass-through).

  • Sorting Functions (They have a slow performance if it's not pass-through! - https://wiki.php.net/rfc/stable_sorting)
    • asort, arsort, ksort, krsort, natcasesort, natsort, rsort, sort, uasort, uksort, usort
  • String Functions
    • str_contains, str_ends_with, str_starts_with

Useful Functions

  • Array Functions - Functions to optimize working with arrays
    • array_compare_flags, array_move_element, array_group_by, array_prefix_add, array_prefix_remove
  • Bitwise Functions - Some useful functions to work with sets of bits.
    • bit_set, bit_isset, bit_unset
  • Reflection Functions - Useful functions to get ReflectionClass, ReflectionMethod and ReflectionProperty on inheritance
    • get_reflection_class, get_reflection_method, get_reflection_property

Examples

Use of array functions

<?php

$array = [128, 2, 16, 64, 8, 1, 32];

// or MUtils\Arrays::userAssociativeSort()
MUtils\Arrays\uasort($array, static function ($left, $right) {
    return $left <=> $right;
})

Use of string functions

<?php

// or MUtils\Strings::startsWith()
if (MUtils\Strings\str_starts_with('Some Text', 'Some')) {
    // ...
}

Use of bit functions

<?php

// or MUtils\Bits::isset()
if (MUtils\Bits\bit_isset(12, 4)) {
    // ...
}

Use of reflection functions

<?php

// or MUtils\Objects::getReflectionMethod()
$instance = new Something();
$method = MUtils\Objects\get_reflection_method($instance, 'foo');
if ($method) {
    $method->invoke($instance, 'arg');
    // ...
}