jQuery’s $.extend() PHP Equivalent for Options

I found myself liking the jQuery’s practice for developing Plugins with jQuery’s $.extend() function, passing user options, stripping out any incorrect options against an array of defaults. Use function below (Delete the private if you prefer it outside an class, but I’ve used it often as part of a constructor) by allowing an user to submit an options array into class. For Example:

<br />
/**<br />
 *   User's Options.<br />
**/<br />
$Options = array(<br />
    &quot;AllowBar&quot; =&gt; true<br />
    &quot;InvalidOpt&quot; =&gt; &quot;Baz&quot;<br />
);</p>
<p>/**<br />
 *   Initialise the Foo class below<br />
 *   with the User's Options.<br />
**/<br />
$FooExample = new Foo( $Options );</p>
<p>class Foo {</p>
<p>    private $ProgramSettings;</p>
<p>    private $DefaultOpts = array(<br />
              &quot;AllowBar&quot; =&gt; false,<br />
              &quot;Color&quot;    =&gt; &quot;White&quot;<br />
            );</p>
<p>    public function __construct( $Opt = array() ) {<br />
        //$.extend the two arrays.<br />
        $this-&gt;ProgramSettings = $this-&gt;StartUpSettings(<br />
                                                $this-&gt;DefaultOpts,<br />
                                                $Opt<br />
                                             );<br />
    }</p>
<p>   /**<br />
    *  Here we '$.extend()' the provided array aganist;<br />
    *  the user's provided options,<br />
    *<br />
    *  This will effectively 'array_merge' the user<br />
    *  &amp; the system settings.<br />
    *<br />
    *  Note, it uses a union merge, not the<br />
    *  php's Array Merge to merge them.<br />
   **/<br />
    private function StartUpSettings( $Defaults, $Opt ) {<br />
           //For each Option, their Key =&gt; Value<br />
           foreach ( $Opt as $k =&gt; $v ):<br />
               if ( !array_key_exists( $k, $Defaults ))<br />
                   unset( $Opt[$k] );<br />
           endforeach;<br />
           $this-&gt;Settings = $Opt + $Defaults;<br />
           //Return the stripped Array<br />
           return $this-&gt;Settings;<br />
   }</p>
<p>}<br />

After `StartUpSettings` is executed, this will re-produce an array alike:

<br />
   /** After the &quot;$.extend()&quot; **/<br />
   print_r( $this-&gt;ProgramSettings );<br />
   Array<br />
   (<br />
       &quot;AllowBar&quot; =&gt; true,<br />
       &quot;Color&quot;    =&gt; &quot;White&quot;<br />
   );<br />