title: PHP: Sending a list of options as an argument (alternative to named parameters/ argument bag)
body: <p>I wish to give a list of options as an argument to a function.</p>
  
  <h2>The Ideal Scenario: Named Parameters</h2>
  
  <p>If PHP has named parameters it would be done like so:</p>
  
  <pre><code>function setOptions($title, $url, $public = true, $placeholder = "type here...") {
     ...
  }
  
  setOptions($title = "Hello World", $url = "example.com", $placeholder = "hi"); 
  </code></pre>
  
  <p>Unfortunately PHP does <strong>not</strong> have named parameters <em>(please tell me if PHP7 is planned to have some as a comment)</em>.</p>
  
  <h2>The solution everyone else is using: Associative Array</h2>
  
  <p>Most PHP scripts I have seen use an alternative array approach like so:</p>
  
  <pre><code>function setOptions($options) {
     ...
  }
  
  setOptions(array(
     'title' =&gt; "Hello World",
     'url' =&gt; "example.com",
     'placeholder' =&gt; "hi"
  ));
  </code></pre>
  
  <h2>Drawbacks of Associative Array Approach</h2>
  
  <p>Although this works fine, there are the following drawbacks:</p>
  
  <ul>
  <li>The user does not benefit from autocompletion (taking a long time to write)</li>
  <li>The user can easily makes mistakes in spellings</li>
  <li>The don't know what options is available, so may frequently revert back to documentation</li>
  </ul>
  
  <h2>Is there a better way?</h2>
  
  <p>Is there a better way that can address these issues (either in current PHP or PHP7 or maybe even hacklang(?)).</p>