# 在一个类中调用array\_map函数

**URL:** https://www.shiqidu.com/t/topic/437
**Category:** 开发调优
**Tags:** PHP
**Created:** [2018 年6 月 9 日 06:26 UTC](https://www.shiqidu.com/t/topic/437 "2018-06-09T06:26:18Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### 作者： ![Eureka](https://www.shiqidu.com/user_avatar/www.shiqidu.com/eureka/32/5_2.png) [@Eureka](https://www.shiqidu.com/u/Eureka)
#### 发布日期： [2018 年6 月 9 日 06:26 UTC](https://www.shiqidu.com/t/topic/437/1 "2018-06-09T06:26:18Z")

</div>

在函数中调用

```php
function inputFilter($data)
{
    if (empty($data)) {
        return '';
    }
    if (is_array($data)) {
        return array_map('inputFilter', $data);
    } else {
        return addslashes(stripslashes(trim(strip_tags($data))));
    }
}

```

在类中调用

```php
class a {
    public static function inputFilter($data)
    {
        if (empty($data)) {
            return '';
        }
        if (is_array($data)) {
            return array_map([__CLASS__ , 'inputFilter'], $data);
        } else {
            return addslashes(stripslashes(trim(strip_tags($data))));
        }
    }
}

```

使用` __CLASS__ `加方法名组合的数组作为array\_map的第一个参数，如果你的方法是非静态的还可以使用`$this` 。

```php
class a {
    public static function inputFilter($data)
    {
        if (empty($data)) {
            return '';
        }
        if (is_array($data)) {
            return array_map([$this, 'inputFilter'], $data);
        } else {
            return addslashes(stripslashes(trim(strip_tags($data))));
        }
    }
}

```
