What scenarios is the php callback function mainly used in?

Recently, I have been studying the callback function, which is said to be one of the necessary skills to become a senior programmer. I searched the Internet for most of the day, but until now I haven"t figured out the real scene of the callback, so I searched the company"s laravel project with the call_user_func keyword, N callbacks, and didn"t understand it. Take the following code as an example:

/**
 * Asserts that a haystack that is stored in a static attribute of a class
 * or an attribute of an object contains a needle.
 *
 * @param mixed  $needle
 * @param string $haystackAttributeName
 * @param mixed  $haystackClassOrObject
 * @param string $message
 * @param bool   $ignoreCase
 * @param bool   $checkForObjectIdentity
 * @param bool   $checkForNonObjectIdentity
 */
function assertAttributeContains($needle, $haystackAttributeName, $haystackClassOrObject, $message = "", $ignoreCase = false, $checkForObjectIdentity = true, $checkForNonObjectIdentity = false)
{
    return call_user_func_array(
        "PHPUnit_Framework_Assert::assertAttributeContains",
        func_get_args()
    );
}

this code uses callbacks, but it"s not clear that it can"t be written properly. As follows:

function assertAttributeContains($needle, $haystackAttributeName, $haystackClassOrObject, $message = "", $ignoreCase = false, $checkForObjectIdentity = true, $checkForNonObjectIdentity = false)
{
    return PHPUnit_Framework_Assert::assertAttributeContains(func_get_args());
}

Why not use this direct call instead of a callback? It feels a little pretentious.

Please give me some advice, thank you!


refer to the same question

php-what's the difference between using call_user_func_array () to execute a function back and forth and using a function directly? -SegmentFault think

recently used multiple processes in a project. Each process needs to assign a method to throw to the process for execution. In this case, you need to pass the closure function as a parameter, and then call it in the child process! You can see that all cases where you need to use a function or method as a parameter for another object to execute need to use a closure function!


this question is a little difficult to answer

first of all, most direct calls can be called directly instead of call_user_func and call_user_func_array

these two functions, the function called can be anonymous, class object method, class static method

Closure anonymous class, which will be used in many subsequent places
use can cause context variables, which is the missing part of php

.

for more places, you can refer to the official php

.

I think the usage here is just to simplify the process of passing parameters. In addition, there is something wrong with your rewriting method. It should be

return PHPUnit_Framework_Assert::assertAttributeContains (.func _ get_args ());

in versions of PHP without the . operator, if you don't use call_user_func_array , you can only pass each parameter manually.


the code owner can only say that the person who wrote the code did not use it to the extreme.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1be2231-30e29.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1be2231-30e29.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?