How do I compare two callable types to check if they are equal or the same?
function addCallable(callable $cb)
{
if(/*already exists*/)
throw new Exception("Callable was already added to the collection");
else
$this->collection[] = $cb;
}
function removeCallable(callable $cb)
{
$key = array_search(/* ??? */);
unset($this->collection[$key]);
}
$this->addCallable(array('MyClass', 'myCallbackMethod'));
try{ $this->addCallable('MyClass::myCallbackMethod'); }catch(Exception $e){}
$this->removeCallable('MyClass::myCallbackMethod');
thank you very much
You could use the third parameter of is_callable function to get the callable name, which is a string.
If the callable is array('MyClass', 'myCallbackMethod')
, then the callable name will be 'MyClass::myCallbackMethod'
.
function addCallable(callable $cb)
{
is_callable($cb, true, $callable_name);
if(isset($this->collection[$callable_name])) {
throw new Exception("Callable was already added to the collection");
} else {
$this->collection[$callable_name] = $cb;
}
}
function removeCallable(callable $cb)
{
is_callable($cb, true, $callable_name);
unset($this->collection[$callable_nam]);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With