sfCallable.class.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfCallable represents a PHP callable.
  11. *
  12. * @package symfony
  13. * @subpackage util
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfCallable.class.php 11534 2008-09-14 14:35:50Z fabien $
  16. */
  17. class sfCallable
  18. {
  19. protected
  20. $callable = null;
  21. /**
  22. * Constructor.
  23. *
  24. * @param mixed $callable A valid PHP callable (must be valid when calling the call() method)
  25. */
  26. public function __construct($callable)
  27. {
  28. $this->callable = $callable;
  29. }
  30. /**
  31. * Returns the current callable.
  32. *
  33. * @param mixed The current callable
  34. */
  35. public function getCallable()
  36. {
  37. return $this->callable;
  38. }
  39. /**
  40. * Calls the current callable with the given arguments.
  41. *
  42. * The callable is called with the arguments given to this method.
  43. *
  44. * This method throws an exception if the callable is not valid.
  45. * This check is not done during the object construction to allow
  46. * you to load the callable as late as possible.
  47. */
  48. public function call()
  49. {
  50. if (!is_callable($this->callable))
  51. {
  52. throw new sfException(sprintf('"%s" is not a valid callable.', is_array($this->callable) ? sprintf('%s:%s', is_object($this->callable[0]) ? get_class($this->callable[0]) : $this->callable[0], $this->callable[1]) : (is_object($this->callable) ? sprintf('Object(%s)', get_class($this->callable)) : var_export($this->callable, true))));
  53. }
  54. $arguments = func_get_args();
  55. return call_user_func_array($this->callable, $arguments);
  56. }
  57. }