最近在研究回调函数,据说是变身高级程序员必备技能之一,网上搜了大半天,到现在一直也没搞清楚回调使用的真正场景,遂在公司的laravel项目中以call_user_func为关键字搜了下,N多回调,没看懂,以下面代码为例:
/**
* 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()
);
}
这段代码使用了回调,但是搞不清楚不能正常的写吗?如下:
function assertAttributeContains($needle, $haystackAttributeName, $haystackClassOrObject, $message = '', $ignoreCase = false, $checkForObjectIdentity = true, $checkForNonObjectIdentity = false)
{
return PHPUnit_Framework_Assert::assertAttributeContains(func_get_args());
}
为何不用这种直接调用而非得用回调呢?感觉有点装逼。
请大神指点,多谢!
慕标琳琳
叮当猫咪
GCT1015
凤凰求蛊
海绵宝宝撒