<?php function namedParams (array $params, array $defaults){ $params += $defaults; $allowedKeys = array_keys($defaults); $isUnexpected = array_filter($params, function ($k) use ($allowedKeys){ return !in_array($k, $allowedKeys); }, ARRAY_FILTER_USE_KEY); if(!empty($isUnexpected)){ throw new InvalidArgumentException('Unexpected parameters: '.implode(',',array_keys($isUnexpected))); } return $params; } function test (array $params){ //we can try ... and catch any problems if we want, but to keep it simple, we can just do this. //These and only these params are expected. Plus, we give them default values. $paramsProcessed = namedParams($params,[ 'name' => 'Unknown', 'sex' => 'man', 'something' => NULL, ]); var_dump($paramsProcessed); } test(['name' => "Konrad"]);
You have javascript disabled. You will not be able to edit any code.