Oihana PHP

isCallableWithParams.php

Table of Contents

Functions

isCallableWithParams()  : bool
Check if an array represents a callable with parameters in simplified syntax.

Functions

isCallableWithParams()

Check if an array represents a callable with parameters in simplified syntax.

isCallableWithParams(array<string|int, mixed> $array[, array<string|int, mixed>|null $validNames = null ]) : bool

Distinguishes between:

  • ['substring', 0, 3] → callable with params (true)
  • ['trim', 'lower'] → chain of callables (false)
  • [['trim', 1], 'lower'] → chain of callables (false)
  • ['lower'] → single callable without params (false)

This is useful for supporting simplified syntax in configurations:

  • Simplified: ['functionName', param1, param2]
  • Explicit: [['functionName', param1, param2]]

Detection Rules:

  1. Must have at least 2 elements (callable name + at least one param)
  2. First element must be a string (callable name)
  3. Optionally validate against a list of known callable names
  4. If second element is also a known callable name → it's a chain (false)
  5. If second element is an array → it's a chain (false)
  6. Otherwise → it's a callable with params (true)
Parameters
$array : array<string|int, mixed>

Array to check

$validNames : array<string|int, mixed>|null = null

Optional list of valid callable names to validate against. If provided, both the first element and second element (if string) will be checked against this list.

Tags
example
use function oihana\core\arrays\isCallableWithParams;

// Without validation
isCallableWithParams(['substring', 0, 3]);
// Returns: true

// With validation
$validFunctions = ['trim', 'lower', 'upper', 'substring'];

isCallableWithParams(['substring', 0, 3], $validFunctions);
// Returns: true (substring is valid, params provided)

isCallableWithParams(['trim', 'lower'], $validFunctions);
// Returns: false (both are valid function names → chain)

isCallableWithParams(['unknown', 0, 3], $validFunctions);
// Returns: false (unknown is not in valid list)

isCallableWithParams(['lower'], $validFunctions);
// Returns: false (no params provided)

// Real-world usage
$config = ['cache', 'redis', 3600];
if (isCallableWithParams($config, ['cache', 'log', 'queue']))
{
    [$method, ...$params] = $config;
    $this->$method(...$params);
}
author

Marc Alcaraz (eKameleon)

version
1.0.8
Return values
bool

True if it's a single callable with parameters


        
On this page

Search results