Last Updated: February 25, 2016
·
12.97K
· macodev

Use php array_map in classes

PHP array_map function can be used to map an array with class methods.

Here is an example:

<?php

class Form{

    public function sanitizeData()
    {
        $data = [
            ' this is a text with extra spaces ',
            'and <strong>this</strong> has too<br>much tags'
        ];
        return array_map(array($this, 'sanitize'), $data);
    }

    private function sanitize($value)
    {
        return strip_tags(trim($value));
    }
}

$form = new Form();

var_dump($form->sanitizeData());

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

PHP applications still send a browser full of JavaScript. Hardening the server does not inventory the payment, analytics, and plugin scripts that run after the HTML is delivered.

Security insights from Cside