Skip to content

Instantly share code, notes, and snippets.

@idev247
Created January 25, 2013 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idev247/4634377 to your computer and use it in GitHub Desktop.
Save idev247/4634377 to your computer and use it in GitHub Desktop.
Trick to get options from default values.
<?php
class TestStuff {
private $defaults = array(
'color' => '#00ff00'
);
public function doIt() {
return $this->__request(array('color' => '#ff0000'));
}
public function getIt() {
return $this->__request();
}
private function __request(array $options = array()) {
$options = array_filter(array_intersect_key(array_merge($this->defaults, $options), $this->defaults));
echo $options; // from doIt it would echo '#ff0000' but from getIt it would be '#00ff00'
}
}
@xeoncross
Copy link

function (array $options)
{
    $defaults = array(...);
    $options = $options + $defaults;
    return $options['key'];
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment