use Symfony\Component\OptionsResolver\OptionsResolverInterface; public function myMethod(array $options = []) { $resolver = new OptionsResolverInterface; $resolver->setOptional(['age']); $resolvedOptions = $resolver->resolve($options); // Your code using $resolvedOptions['name'] and $resolvedOptions['age'] }
use Symfony\Component\OptionsResolver\OptionsResolverInterface; class MyService { public function __construct(array $options) { $resolver = new OptionsResolverInterface; $resolver->setRequired(['connection']); $resolver->setOptional(['timeout']); $resolvedOptions = $resolver->resolve($options); // Your code using $resolvedOptions['connection'] and $resolvedOptions['timeout'] } }This sets the 'connection' option as required and the 'timeout' option as optional. This means that calling the constructor without providing a 'connection' value will generate an error, but calling it without providing a 'timeout' value will not generate an error.