php array_map with static method of object

Samuel Dauzon picture Samuel Dauzon · Jan 26, 2016 · Viewed 12.6k times · Source

I want to use array_map with a static method but I fail. Here is my code :

Class Buy {

    public function payAllBills() {
        $bill_list = OtherClass::getBillList();
        return array_map(array(self, 'pay'), $bill_list); // Issue line
    }

    private static function pay($bill) {
        // Some stuff
        return true;
    }

}

PHP gives me the error :

Use of undefined constant self - assumed 'self'

I have also tried :

return array_map('self::makeBean()', $model_list);

But it doesn't work.

Do you have any idea how to use array_map with static method ?

I have already read : Can a method be used as a array_map function in PHP 5.2? but this question is about standard methods, not statics.

Answer

Mark Baker picture Mark Baker · Jan 26, 2016

As per the documentation,

return array_map('self::pay', $model_list);

Note that your attempt included () in the method name string, which would be wrong