PHP code example of mordilion / mutils

1. Go to this page and download the library: Download mordilion/mutils library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mordilion / mutils example snippets


// Stable sorting (on PHP < 8.0: guaranteed stable, on PHP 8.0+: native pass-through)
$array = [3 => 'b', 1 => 'a', 2 => 'a'];
MUtils\Arrays\asort($array);

// String checks (PHP 8.0+ backport)
MUtils\Strings\str_starts_with('Hello World', 'Hello'); // true

// Bitwise operations
$flags = MUtils\Bits\bit_set(0, 4);    // 4
MUtils\Bits\bit_isset($flags, 4);      // true

// Reflection with inheritance traversal
$method = MUtils\Objects\get_reflection_method($object, 'parentMethod');

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

MUtils\Arrays\sort($array);
// [1, 2, 8, 16, 32, 64, 128]

// Or using the OOP API:
MUtils\Arrays::sort($array);

$data = [
    ['dept' => 'engineering', 'role' => 'backend',  'name' => 'Alice'],
    ['dept' => 'engineering', 'role' => 'frontend', 'name' => 'Bob'],
    ['dept' => 'design',      'role' => 'ux',       'name' => 'Carol'],
];

// Simple grouping
$byDept = MUtils\Arrays\array_group_by($data, false, 'dept');
// ['engineering' => [...], 'design' => [...]]

// Nested grouping
$byDeptAndRole = MUtils\Arrays\array_group_by($data, false, 'dept', 'role');
// ['engineering' => ['backend' => [...], 'frontend' => [...]], 'design' => ['ux' => [...]]]

// With preserved keys
$byDept = MUtils\Arrays\array_group_by($data, true, 'dept');

$array = ['a', 'b', 'c', 'd'];

MUtils\Arrays\array_move_element($array, 3, 1);
// ['a', 'd', 'b', 'c']

$array = ['name' => 'Alice', 'role' => 'dev'];

// Prefix keys (default)
MUtils\Arrays\array_prefix_add($array, 'user_');
// ['user_name' => 'Alice', 'user_role' => 'dev']

// Prefix values
MUtils\Arrays\array_prefix_add($array, 'prefix_', ARRAY_PREFIX_VALUE);
// ['name' => 'prefix_Alice', 'role' => 'prefix_dev']

$array = ['user_name' => 'Alice', 'user_role' => 'dev'];

MUtils\Arrays\array_prefix_remove($array, 'user_');
// ['name' => 'Alice', 'role' => 'dev']

MUtils\Arrays\array_compare_flags('apple', 'Banana', SORT_STRING | SORT_FLAG_CASE);
// < 0 (case-insensitive string comparison)

$flags = 0;

$flags = MUtils\Bits\bit_set($flags, 4);     // 4  (binary: 100)
$flags = MUtils\Bits\bit_set($flags, 8);     // 12 (binary: 1100)

MUtils\Bits\bit_isset($flags, 4);            // true
MUtils\Bits\bit_isset($flags, 2);            // false

$flags = MUtils\Bits\bit_unset($flags, 4);   // 8  (binary: 1000)

$text = 'Hello World';

MUtils\Strings\str_contains($text, 'World');      // true
MUtils\Strings\str_starts_with($text, 'Hello');   // true
MUtils\Strings\str_ends_with($text, 'World');     // true

// Or using the OOP API:
MUtils\Strings::contains($text, 'World');          // true

class ParentClass {
    protected string $secret = 'hidden';

    protected function greet(): string {
        return 'hello';
    }
}

class ChildClass extends ParentClass {}

$child = new ChildClass();

// Finds the method defined in ParentClass
$method = MUtils\Objects\get_reflection_method($child, 'greet');
$method->invoke($child); // 'hello'

// Finds the property defined in ParentClass
$prop = MUtils\Objects\get_reflection_property($child, 'secret');
$prop->setAccessible(true);
$prop->getValue($child); // 'hidden'