<?php
namespace
{
function callable_equals(){
$strict = true;
$callables = func_get_args();
if(is_bool($callables[count($callables) - 1])){
$strict = $callables[count($callables) - 1];
$callables = array_splice($callables, 0, -1);
}
$normalizeCallable = function(callable $callable){
if(is_string($callable)){
$callable = strtolower($callable);
$pieces = explode("::", $callable);
if(count($pieces) == 2){
return [$pieces[0], $pieces[1]];
}
return $callable;
}
if(is_string($callable[0])){
$callable[0] = strtolower($callable[0]);
}
$callable[1] = strtolower($callable[1]);
return $callable;
};
if(count($callables) < 2){
trigger_error("callable_equals() requires at least 2 callables for comparison.", E_USER_WARNING);
return null;
}
foreach($callables as $i => $callable){
if(!is_callable($callable)){
trigger_error("Argument " . ($i + 1) . " is not a callable.", E_USER_WARNING);
return null;
}else{
$callables[$i] = $normalizeCallable($callable);
}
}
$callable = array_shift($callables);
while($callables){
if($strict ? $callable !== $callables[0] : $callable != $callables[0])
return false;
$callable = array_shift($callables);
}
return true;
}
}
namespace A\b\C\d
{
class Foo{static function bar(){}}
}
namespace
{
class Foo{static function bar(){}}
var_dump(callable_equals(
['Foo', 'bar'],
'foo::BAR'
));
var_dump(callable_equals(
['A\\b\\C\\d\\Foo', 'bAr'],
'a\\B\\c\\D\\FoO::bar',
['A\\b\\c\\d\\foo', 'BAR']
));
var_dump(callable_equals(
[new ArrayObject, 'append'],
[new ArrayObject, 'append']
));
}
- Output for 5.4.0 - 5.4.45, 5.5.24 - 5.5.35, 5.6.7 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
- bool(true)
bool(true)
bool(false)
- Output for 5.3.0 - 5.3.29
- Parse error: syntax error, unexpected '[' in /in/ukCeH on line 18
Process exited with code 255. - Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
- Parse error: syntax error, unexpected '{' in /in/ukCeH on line 3
Process exited with code 255. - Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
- Parse error: parse error, unexpected '{' in /in/ukCeH on line 3
Process exited with code 255. - Output for 4.3.2 - 4.3.4
- Parse error: parse error in /in/ukCeH on line 3
Process exited with code 255.
preferences:
218.03 ms | 401 KiB | 343 Q