Last Updated: February 25, 2016
·
478
· 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.
    }

}