Last Updated: February 25, 2016
·
1.014K
· dfeddad

Use array_map to call a method in class with multiple params

class FileUploader {

    /**
     * Upload a single or multiple files to specific target.
     *
     * @param string|array $file
     * @param string $target
     * @return mixed
     */
    public function upload($file, $target)
    {
        if (is_array($file)) {
            return array_map(
                [$this, 'uploadSingle'], 
                $file, 
                array_fill(0, count($file), $target)
            );
        }
        return $this->uploadSingle($file, $target);
    }

    /**
     * Upload a single file to a specific target.
     *
     * @param $file
     * @param $target
     */
    public function uploadSingle($file, $target)
    {
        // Code to handle the file upload.
    }

}

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