Can a method be used as an array_map function Can a method be used as an array_map function php php

Can a method be used as an array_map function


Yes, you can have callbacks to methods, like this:

array_map(array($instance, 'fun'), $ar)

see the callback type in PHP's manual for more info


You can also use

array_map('Class::method', $array) 

syntax.


Actually, you need to know the definition of Callback, please kindly refer to the following code:

<?php // An example callback functionfunction my_callback_function() {    echo 'hello world!';}// An example callback methodclass MyClass {    static function myCallbackMethod() {        echo 'Hello World!';    }}$myArray = [1, 2, 3, 4];// Type 1: Simple callbackarray_map('my_callback_function', $myArray); // Type 2: Static class method callarray_map(array('MyClass', 'myCallbackMethod'), $myArray); // Type 3: Object method call$obj = new MyClass();array_map(array($obj, 'myCallbackMethod'), $myArray);// Type 4: Static class method call (As of PHP 5.2.3)array_map('MyClass::myCallbackMethod', $myArray);// Type 5: Relative static class method call (As of PHP 5.3.0)class A {    public static function who() {        echo "A\n";    }}class B extends A {    public static function who() {        echo "B\n";    }}array_map(array('B', 'parent::who'), $myArray); // A?>

From: http://php.net/manual/en/language.types.callable.php