array_map PHP的设置范围 [英] setting scope of array_map php

查看:223
本文介绍了array_map PHP的设置范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

hey all, i use array_map from time to time to write recursive methods. for example

function stripSlashesRecursive( $value ){

    $value = is_array($value) ?
        array_map( 'stripSlashesRecursive', $value) :
    stripslashes( $value );
    return $value;
}

问:

说我想把这个功能在静态类中,我如何使用array_map回到静态方法的范围像消毒:: stripSlashesRecursive类();
我敢肯定,这是简单,但我只是不能figgure出来,看着php.net为好。

say i wanna put this function in a static class, how would i use array_map back to the scope of the static method in the class like Sanitize::stripSlashesRecursive(); Im sure this is simple but i just cant figgure it out, looked at php.net as well.

推荐答案

在使用一个类的方法,比如的功能的回调array_map() usort(),你必须发送回调的二值数组。第二值始终方法作为字符串的名称。第一个值是上下文(类名或对象)

When using a class method as a callback for functions like array_map() and usort(), you have to send the callback as two-value array. The 2nd value is always the name of the method as a string. The 1st value is the context (class name or object)

// 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 );

这篇关于array_map PHP的设置范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆