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.
$isCountable = Obj\implementsInterface(\Countable::class);
$isCountable(new \ArrayObject()); // true
$isCountable(new \stdClass()); // false
array_filter($mixed, $isCountable); // only the Countable onesAlso: isInstanceOf, usesTrait.
$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 skippedtoArray() takes no arguments and returns a Closure — drop it straight into array_map to convert a list of objects to arrays.
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'