pranay
pranay

Reputation: 474

use of callable in php

I am trying to mimic php's inbuilt usort function definition in my implementation below:

class heapSort {
    static function hsort(array &$array, callable $cmp_function){
       // logic
    }
}

class utility{
    static function mycomparator(){
        // logic
    }
}

$array = array(5,3,8,1);
$callback = array('utility','mycomparator');
heapSort::hsort($array, $callback);

While the variable $callback is "callable" why do I get below fatal error?

Argument 2 passed to heapSort::hsort() must be an instance of callable.

More specifically, how do I make/typecast a $variable to callable?

Upvotes: 4

Views: 5521

Answers (1)

Baba
Baba

Reputation: 95111

callable is only supported PHP 5.4 try using is_callable instead

static function hsort(array &$array, $cmp_function) {
    if (! is_callable($cmp_function))
        throw new InvalidArgumentException("Function not callable");
}

Upvotes: 5

Related Questions