Skip to content

Instantly share code, notes, and snippets.

@jubstuff
Last active August 29, 2015 14:19
Show Gist options
  • Select an option

  • Save jubstuff/dc7d39dea351d6e3e1a1 to your computer and use it in GitHub Desktop.

Select an option

Save jubstuff/dc7d39dea351d6e3e1a1 to your computer and use it in GitHub Desktop.
Filter Storage
<?php
/**
* Stores a value and calls any existing function with this value.
*/
class FilterStorage
{
/**
* Filled by __construct(). Used by __call().
*
* @type mixed Any type you need.
*/
protected $values;
/**
* Stores the values for later use.
*
* @param mixed $values
* @return void
*/
public function __construct( $values )
{
$this->values = $values;
}
/**
* Catches all function calls except __construct().
*
* Be aware: Even if the function is called with just one string as an
* argument it will be sent as an array.
*
* @param string $callback Function name
* @param array $arguments
* @return mixed
*/
public function __call( $callback, $arguments )
{
if ( is_callable( $callback ) )
{
return call_user_func( $callback, $arguments, $this->values );
}
// Wrong function called.
throw new InvalidArgumentException(
sprintf(
'File: %1$s<br>
Line %2$d<br>
There is no function %3$s',
__FILE__,
__LINE__,
$callback
)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment