设置array_map php的范围

Dav*_*row 3 php arrays recursion array-map

嘿所有,我不时使用array_map来编写递归方法.例如

function stripSlashesRecursive( $value ){

    $value = is_array($value) ?
        array_map( 'stripSlashesRecursive', $value) :
    stripslashes( $value );
    return $value;
}
Run Code Online (Sandbox Code Playgroud)

题:

说我想把这个函数放在一个静态类中,我如何使用array_map回到类中静态方法的范围,如Sanitize :: stripSlashesRecursive(); 我确定这很简单,但我不能把它想象出来,看看php.net也是如此.

Pet*_*ley 16

当使用类方法作为array_map()和等函数的回调时usort(),您必须将回调作为双值数组发送.第二个值始终是作为字符串的方法名称.第一个值是上下文(类名或对象)

// Static outside of class context
array_map( array( 'ClassName', 'methodName' ), $array );

// Static inside class context
array_map( array( __CLASS__, 'methodName' ), $array );

// Non-static outside of object context
array_map( array( $object, 'methodName' ), $array );

// Non-static inside of object context
array_map( array( $this, 'methodName' ), $array );
Run Code Online (Sandbox Code Playgroud)