Objects

Small namespace. Four reflection-style predicates and one object factory. All curried and compose cleanly with the Arrays filter/map family for type-driven collection processing.

Type predicates

$isCountable = Obj\implementsInterface(\Countable::class);

$isCountable(new \ArrayObject());     // true
$isCountable(new \stdClass());        // false

array_filter($mixed, $isCountable);   // only the Countable ones

Also: isInstanceOf, usesTrait.

toArray

$user = new class {
    public $name = 'Ada';
    public $role = 'admin';
    private $secret = 'hidden';
};

$toArr = Obj\toArray();

$toArr($user);   // ['name' => 'Ada', 'role' => 'admin']
                 // — $secret is private, so it's skipped

toArray() takes no arguments and returns a Closure — drop it straight into array_map to convert a list of objects to arrays.

createWith

class User {
    public string $name   = '';
    public string $role   = 'user';
    public bool   $active = true;
}

$makeUser = Obj\createWith(User::class, ['role' => 'user', 'active' => true]);

$admin  = $makeUser(['name' => 'Ada', 'role' => 'admin']);  // role overridden
$member = $makeUser(['name' => 'Bea']);                      // inherits base

$admin->role;    // 'admin'
$member->role;   // 'user'

Object Functions