Phly Documentation Phly
[ return to channel ] [ class tree: Phly ] [ index: Phly ] [ all elements ]

Source for file Exception.php

Documentation is available at Exception.php

  1. <?php
  2. /**
  3. * PHLY - PHp LibrarY
  4. *
  5. * PHLY is a library of PHP classes designed with the following intentions:
  6. * - Loosely coupled; dependencies should be few, and no base class should be
  7. * necessary.
  8. * - Extendible; all classes should be easily extendible. This may be via
  9. * observers, interfaces, adapters, etc.. The base class should solve 80% of
  10. * usage, and allow extensions to the class to fill in the remainder.
  11. * - Designed for PHP5 and up; all classes should make use of PHP5's features.
  12. * - Documented; all classes should minimally have excellent API-level
  13. * documentation, with use cases in the class docblock.
  14. * - Tested; all classes should have (passing) unit tests accompanying them.
  15. * - Open source and commercial friendly; all classes should use a
  16. * commercial-friendly open source license. The BSD license is one such
  17. * example.
  18. *
  19. * @license New BSD, http://www.opensource.org/licenses/bsd-license.php
  20. * @package Phly
  21. * @copyright 2006 - Present, Matthew Weier O'Phinney
  22. */
  23.  
  24. /**
  25. * Phly Exception Handling
  26. *
  27. * Phly_Exception implements an Observer pattern. You may build observer
  28. * classes that receive notifications when Phly_Exceptions (or classes
  29. * derived from it) are raised. Such classes need only have an event() method
  30. * that accepts a Phly_Exception object, and have to register their class or
  31. * an instance with Phly_Exception using {@link attach()}.
  32. *
  33. * @category Phly
  34. * @subpackage Phly_Exception
  35. * @copyright 2006 - Present, Matthew Weier O'Phinney
  36. * @author Matthew Weier O'Phinney <mweierophinney@gmail.com>
  37. * @version @release-version@
  38. */
  39. class Phly_Exception extends Exception
  40. {
  41. /**
  42. * Array of observers
  43. * @var array
  44. * @static
  45. * @access protected
  46. */
  47. private static $observers = array();
  48.  
  49. /**
  50. * Constructor
  51. *
  52. * @access public
  53. * @param string
  54. * @param int
  55. * @param int
  56. */
  57. public function __construct($message, $code = 0)
  58. {
  59. parent::__construct($message, $code);
  60.  
  61. $this->notify();
  62. }
  63.  
  64. /**
  65. * String representation of exception
  66. *
  67. * @access public
  68. * @return void
  69. */
  70. public function __toString()
  71. {
  72. return __CLASS__ . ': [' . $this->code . ']: ' . $this->message . "\n";
  73. }
  74.  
  75. /**
  76. * Attach an observer
  77. *
  78. * @static
  79. * @access public
  80. * @param string|object $class Classname or object
  81. * @return void
  82. */
  83. final public static function attach($class)
  84. {
  85. if (is_object($class) || class_exists($class)) {
  86. array_push(self::$observers, $class);
  87. }
  88. }
  89.  
  90. /**
  91. * Notify observers of an error event
  92. *
  93. * @access public
  94. * @return void
  95. */
  96. final public function notify()
  97. {
  98. foreach (self::$observers as $class) {
  99. if (is_callable(array($class, 'event'))) {
  100. call_user_func(array($class, 'event'), $this);
  101. }
  102. }
  103. }
  104. }