PHP PEAR 基类程序阅读与注释

来源:互联网 发布:samba 端口 被屏蔽 编辑:程序博客网 时间:2024/05/20 16:11
 
  1. <?php
  2. /**
  3.  * PEAR, the PHP Extension and Application Repository
  4.  *
  5.  * PEAR class and PEAR_Error class
  6.  *
  7.  * PHP versions 4 and 5
  8.  *
  9.  * LICENSE: This source file is subject to version 3.0 of the PHP license
  10.  * that is available through the world-wide-web at the following URI:
  11.  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  12.  * the PHP License and are unable to obtain it through the web, please
  13.  * send a note to license@php.net so we can mail you a copy immediately.
  14.  *
  15.  * @category   pear
  16.  * @package    PEAR
  17.  * @author     Sterling Hughes <sterling@php.net>
  18.  * @author     Stig Bakken <ssb@php.net>
  19.  * @author     Tomas V.V.Cox <cox@idecnet.com>
  20.  * @author     Greg Beaver <cellog@php.net>
  21.  * @copyright  1997-2006 The PHP Group
  22.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  23.  * @version    CVS: $Id: PEAR.php,v 1.101 2006/04/25 02:41:03 cellog Exp $
  24.  * @link       http://pear.php.net/package/PEAR
  25.  * @since      File available since Release 0.1
  26.  */
  27. /**#@+
  28.  * ERROR constants
  29.  */
  30. define('PEAR_ERROR_RETURN',     1);
  31. define('PEAR_ERROR_PRINT',      2);
  32. define('PEAR_ERROR_TRIGGER',    4);
  33. define('PEAR_ERROR_DIE',        8);
  34. define('PEAR_ERROR_CALLBACK',  16);
  35. /**
  36.  * WARNING: obsolete
  37.  * @deprecated
  38.  */
  39. define('PEAR_ERROR_EXCEPTION', 32);
  40. /**#@-*/
  41. define('PEAR_ZE2', (function_exists('version_compare') &
  42.                     version_compare(zend_version(), "2-dev""ge")));
  43. if (substr(PHP_OS, 0, 3) == 'WIN') {
  44.     define('OS_WINDOWS', true);
  45.     define('OS_UNIX',    false);
  46.     define('PEAR_OS',    'Windows');
  47. else {
  48.     define('OS_WINDOWS', false);
  49.     define('OS_UNIX',    true);
  50.     define('PEAR_OS',    'Unix'); // blatant assumption
  51. }
  52. // instant backwards compatibility
  53. if (!defined('PATH_SEPARATOR')) {
  54.     if (OS_WINDOWS) {
  55.         define('PATH_SEPARATOR'';');
  56.     } else {
  57.         define('PATH_SEPARATOR'':');
  58.     }
  59. }
  60. $GLOBALS['_PEAR_default_error_mode']     = PEAR_ERROR_RETURN;
  61. $GLOBALS['_PEAR_default_error_options']  = E_USER_ALL;
  62. $GLOBALS['_PEAR_destructor_object_list'] = array();
  63. $GLOBALS['_PEAR_shutdown_funcs']         = array();
  64. $GLOBALS['_PEAR_error_handler_stack']    = array();
  65. @ini_set('track_errors', true);
  66. /**
  67.  * Base class for other PEAR classes.  Provides rudimentary
  68.  * emulation of destructors.
  69.  *
  70.  * If you want a destructor in your class, inherit PEAR and make a
  71.  * destructor method called _yourclassname (same name as the
  72.  * constructor, but with a "_" prefix).  Also, in your constructor you
  73.  * have to call the PEAR constructor: $this->PEAR();.
  74.  * The destructor method will be called without parameters.  Note that
  75.  * at in some SAPI implementations (such as Apache), any output during
  76.  * the request shutdown (in which destructors are called) seems to be
  77.  * discarded.  If you need to get any debug information from your
  78.  * destructor, use error_log(), syslog() or something similar.
  79.  *
  80.  * IMPORTANT! To use the emulated destructors you need to create the
  81.  * objects by reference: $obj =& new PEAR_child;
  82.  *
  83.  * @category   pear
  84.  * @package    PEAR
  85.  * @author     Stig Bakken <ssb@php.net>
  86.  * @author     Tomas V.V. Cox <cox@idecnet.com>
  87.  * @author     Greg Beaver <cellog@php.net>
  88.  * @copyright  1997-2006 The PHP Group
  89.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  90.  * @version    Release: 1.6.2
  91.  * @link       http://pear.php.net/package/PEAR
  92.  * @see        PEAR_Error
  93.  * @since      Class available since PHP 4.0.2
  94.  * @link        http://pear.php.net/manual/en/core.pear.php#core.pear.pear
  95.  */
  96. class PEAR
  97. {
  98.     // {{{ properties
  99.     /**
  100.      * Whether to enable internal debug messages.
  101.      *
  102.      * @var     bool
  103.      * @access  private
  104.      */
  105.     var $_debug = false;
  106.     /**
  107.      * Default error mode for this object.
  108.      *
  109.      * @var     int
  110.      * @access  private
  111.      */
  112.     var $_default_error_mode = null;
  113.     /**
  114.      * Default error options used for this object when error mode
  115.      * is PEAR_ERROR_TRIGGER.
  116.      *
  117.      * @var     int
  118.      * @access  private
  119.      */
  120.     var $_default_error_options = null;
  121.     /**
  122.      * Default error handler (callback) for this object, if error mode is
  123.      * PEAR_ERROR_CALLBACK.
  124.      *
  125.      * @var     string
  126.      * @access  private
  127.      */
  128.     var $_default_error_handler = '';
  129.     /**
  130.      * Which class to use for error objects.
  131.      *
  132.      * @var     string
  133.      * @access  private
  134.      */
  135.     var $_error_class = 'PEAR_Error';
  136.     /**
  137.      * An array of expected errors.
  138.      *
  139.      * @var     array
  140.      * @access  private
  141.      */
  142.     var $_expected_errors = array();
  143.     // }}}
  144.     // {{{ constructor
  145.     /**
  146.      * Constructor.  Registers this object in
  147.      * $_PEAR_destructor_object_list for destructor emulation if a
  148.      * destructor object exists.
  149.      *
  150.      * @param string $error_class  (optional) which class to use for
  151.      *        error objects, defaults to PEAR_Error.
  152.      * @access public
  153.      * @return void
  154.      */
  155.      
  156.      /**
  157.       * 解读:PEAR构造方法
  158.       * 1 自动通过当前对象[$this] 获取当前对象类名
  159.       * 2 检查是否打开PEAR DEBUG 模式,如果打开,输出提示信息"PEAR constructor called, class=$classname/n"
  160.       * 3 如果该方法调用时提供了系统错误类"$error_class",该错误类将被注册为PEAR 指定的错误类。
  161.       * 4 如果该类名确实存在,并且比pear字符串长,那么执行循环寻找destructor方法,如果将当前对象引用注册为PEAR系统
  162.       *   解构对象列表,这也意味者,当程序停止,或该类退出使用时,会自动执行当前对象的解构方法。
  163.       * 5 解构方法寻找的名称规则为 "_".$classname,其中$classname 为当前对象类名。
  164.       * 6 在循环体中,检查是否已经将"_PEAR_call_destructors"注册为脚本关闭时调用的函数,如果没有注册那么就立即注册
  165.       *   并且将全局变量"$GLOBALS['_PEAR_SHUTDOWN_REGISTERED']"的值设为"true:boolean",当下次检查时也是根据该
  166.       *   值检查是否PEAR已经注册脚本关闭执行函数"_PEAR_call_destructors"。
  167.       * 7 依次从当前对象开始循环寻找该对象的父对象并执行循环体任务。
  168.       * -------------------------------------------------------------
  169.       * 8 根据脚本关闭的注册和检查方法可以了解PHP脚本无法获取是否已经注册脚本关闭函数的信息,因此在该函数中通过其他方
  170.       *   法来检查,这也意味着,如果系统其它类通过其它途径注册脚本关闭函数,并且不依赖于全局变量,或者在全局变量执行前
  171.       *   注册,那么该注册无效,将被PEAR的脚本关闭函数替换,或者替换掉PEAR的脚本关闭函数。
  172.       *   这里在程序中应该注意,整套系统的系统运行注册的方法应该在系统内部通过统一的方法管理。以确保程序有效执行。
  173.       *   在该方法中通过全局变量"$_PEAR_destructor_object_list"堆栈的方式注册、管理和执行脚本关闭的方法,是可取的
  174.       *   解决脚本关闭时执行函数的方法。通过注册对象引用为全局变量"$_PEAR_destructor_object_list"的内容,这个做法
  175.       *   很聪明,这样使得调用和执行都很方便,只要遵循一定的命名规则就可以了。
  176.       ***************************************************************/
  177.     function PEAR($error_class = null)
  178.     {
  179.         $classname = strtolower(get_class($this));
  180.         if ($this->_debug) {
  181.             print "PEAR constructor called, class=$classname/n";
  182.         }
  183.         if ($error_class !== null) {
  184.             $this->_error_class = $error_class;
  185.         }
  186.         while ($classname && strcasecmp($classname"pear")) {
  187.             //fetch destructor method name
  188.             $destructor = "_$classname";
  189.             if (method_exists($this$destructor)) {
  190.                 global $_PEAR_destructor_object_list;
  191.                 $_PEAR_destructor_object_list[] = $this;
  192.                 if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) {
  193.                     register_shutdown_function("_PEAR_call_destructors");
  194.                     $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'] = true;
  195.                 }
  196.                 break;
  197.             } else {
  198.                 $classname = get_parent_class($classname);
  199.             }
  200.         }
  201.     }
  202.     // }}}
  203.     // {{{ destructor
  204.     /**
  205.      * Destructor (the emulated type of...).  Does nothing right now,
  206.      * but is included for forward compatibility, so subclass
  207.      * destructors should always call it.
  208.      *
  209.      * See the note in the class desciption about output from
  210.      * destructors.
  211.      *
  212.      * @access public
  213.      * @return void
  214.      */
  215.      /**
  216.       * 解读:PEAR解构方法
  217.       *
  218.       * 1 该方法目前没有什么任务,只是输出一句基本没用的信息。该方法的存在只是为了以后的兼容性,因此系统执行时
  219.       *   应该总是调用该方法,这样当需要扩展该方法的时候很容易扩展,而且关联程序不需要做任何处理.
  220.       * 2 程序设计应该有一定的规范性和远见,这个方法的存在是很好的,也非常鼓励这样做.
  221.       *
  222.       ***************************************************************/
  223.     function _PEAR() {
  224.         if ($this->_debug) {
  225.             printf("PEAR destructor called, class=%s/n"strtolower(get_class($this)));
  226.         }
  227.     }
  228.     // }}}
  229.     // {{{ getStaticProperty()
  230.     /**
  231.     * If you have a class that's mostly/entirely static, and you need static
  232.     * properties, you can use this method to simulate them. Eg. in your method(s)
  233.     * do this: $myVar = &PEAR::getStaticProperty('myclass', 'myVar');
  234.     * You MUST use a reference, or they will not persist!
  235.     *
  236.     * @access public
  237.     * @param  string $class  The calling classname, to prevent clashes
  238.     * @param  string $var    The variable to retrieve.
  239.     * @return mixed   A reference to the variable. If not set it will be
  240.     *                 auto initialised to NULL.
  241.     */
  242.     /**
  243.       * 解读:PEA获得类的静态属性
  244.       *
  245.       * 1 这个方法没有看明白怎么运行...
  246.       * 2 这个程序终于看懂了,写PEAR包这些人实在是高手。
  247.       * 3 这个方法是通过引用运行的,方法的返回值是一个变量的引用,虽然上面英文注释也说了,但是刚才实在是没有看明白。
  248.       *   举个例子看一下:
  249.       *   //获取一个静态数组 $properties['myClass']['newVariableName']的引用,实例化时,前面的引用符号是必须的。
  250.       *   $a = & PEAR::getStaticProperty("myClass","newVariableName");
  251.       *   //给引用赋值,这个赋值相当于$properties['myClass']['newVariableName']="hello world";,这个就是引用的艺术了。
  252.       *   $a = "hello world";
  253.       *   //提取$properties['myClass']['newVariableName']的值,没有引用符号也是可以的,但是只限于提取变量内容,不能赋值。
  254.       *   $a_value = PEAR::getStaticProperty("myClass", "newVariableName");
  255.       * 4 这个方法在PEAR包中起到一个插件的作用,凡是调用PEAR包的程序都可以轻松的使用该插件设置和提取变量值,并且该变量
  256.       *   在整个程序运行中都有效,而且变量的有效范围仅限于函数内部,不会与外部变量引起冲突。这个方法真是静态变量、变量作用域和
  257.       *   引用的完美结合。
  258.       ***************************************************************/
  259.     function &getStaticProperty($class$var)
  260.     {
  261.         static $properties;
  262.         if (!isset($properties[$class])) {
  263.             $properties[$class] = array();
  264.         }
  265.         if (!array_key_exists($var$properties[$class])) {
  266.             $properties[$class][$var] = null;
  267.         }
  268.         return $properties[$class][$var];
  269.     }
  270.     // }}}
  271.     // {{{ registerShutdownFunc()
  272.     /**
  273.     * Use this function to register a shutdown method for static
  274.     * classes.
  275.     *
  276.     * @access public
  277.     * @param  mixed $func  The function name (or array of class/method) to call
  278.     * @param  mixed $args  The arguments to pass to the function
  279.     * @return void
  280.     */
  281.     function registerShutdownFunc($func$args = array())
  282.     {
  283.         // if we are called statically, there is a potential
  284.         // that no shutdown func is registered.  Bug #6445
  285.         if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) {
  286.             register_shutdown_function("_PEAR_call_destructors");
  287.             $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'] = true;
  288.         }
  289.         $GLOBALS['_PEAR_shutdown_funcs'][] = array($func$args);
  290.     }
  291.     // }}}
  292.     // {{{ isError()
  293.     /**
  294.      * Tell whether a value is a PEAR error.
  295.      *
  296.      * @param   mixed $data   the value to test
  297.      * @param   int   $code   if $data is an error object, return true
  298.      *                        only if $code is a string and
  299.      *                        $obj->getMessage() == $code or
  300.      *                        $code is an integer and $obj->getCode() == $code
  301.      * @access  public
  302.      * @return  bool    true if parameter is an error
  303.      */
  304.     function isError($data$code = null)
  305.     {
  306.         if (is_a($data'PEAR_Error')) {
  307.             if (is_null($code)) {
  308.                 return true;
  309.             } elseif (is_string($code)) {
  310.                 return $data->getMessage() == $code;
  311.             } else {
  312.                 return $data->getCode() == $code;
  313.             }
  314.         }
  315.         return false;
  316.     }
  317.     // }}}
  318.     // {{{ setErrorHandling()
  319.     /**
  320.      * Sets how errors generated by this object should be handled.
  321.      * Can be invoked both in objects and statically.  If called
  322.      * statically, setErrorHandling sets the default behaviour for all
  323.      * PEAR objects.  If called in an object, setErrorHandling sets
  324.      * the default behaviour for that object.
  325.      *
  326.      * @param int $mode
  327.      *        One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT,
  328.      *        PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE,
  329.      *        PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION.
  330.      *
  331.      * @param mixed $options
  332.      *        When $mode is PEAR_ERROR_TRIGGER, this is the error level (one
  333.      *        of E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR).
  334.      *
  335.      *        When $mode is PEAR_ERROR_CALLBACK, this parameter is expected
  336.      *        to be the callback function or method.  A callback
  337.      *        function is a string with the name of the function, a
  338.      *        callback method is an array of two elements: the element
  339.      *        at index 0 is the object, and the element at index 1 is
  340.      *        the name of the method to call in the object.
  341.      *
  342.      *        When $mode is PEAR_ERROR_PRINT or PEAR_ERROR_DIE, this is
  343.      *        a printf format string used when printing the error
  344.      *        message.
  345.      *
  346.      * @access public
  347.      * @return void
  348.      * @see PEAR_ERROR_RETURN
  349.      * @see PEAR_ERROR_PRINT
  350.      * @see PEAR_ERROR_TRIGGER
  351.      * @see PEAR_ERROR_DIE
  352.      * @see PEAR_ERROR_CALLBACK
  353.      * @see PEAR_ERROR_EXCEPTION
  354.      *
  355.      * @since PHP 4.0.5
  356.      */
  357.     function setErrorHandling($mode = null, $options = null)
  358.     {
  359.         if (isset($this) && is_a($this'PEAR')) {
  360.             $setmode     = $this->_default_error_mode;
  361.             $setoptions  = $this->_default_error_options;
  362.         } else {
  363.             $setmode     = $GLOBALS['_PEAR_default_error_mode'];
  364.             $setoptions  = $GLOBALS['_PEAR_default_error_options'];
  365.         }
  366.         switch ($mode) {
  367.             case PEAR_ERROR_EXCEPTION:
  368.             case PEAR_ERROR_RETURN:
  369.             case PEAR_ERROR_PRINT:
  370.             case PEAR_ERROR_TRIGGER:
  371.             case PEAR_ERROR_DIE:
  372.             case null:
  373.                 $setmode = $mode;
  374.                 $setoptions = $options;
  375.                 break;
  376.             case PEAR_ERROR_CALLBACK:
  377.                 $setmode = $mode;
  378.                 // class/object method callback
  379.                 if (is_callable($options)) {
  380.                     $setoptions = $options;
  381.                 } else {
  382.                     trigger_error("invalid error callback", E_USER_WARNING);
  383.                 }
  384.                 break;
  385.             default:
  386.                 trigger_error("invalid error mode", E_USER_WARNING);
  387.                 break;
  388.         }
  389.     }
  390.     // }}}
  391.     // {{{ expectError()
  392.     /**
  393.      * This method is used to tell which errors you expect to get.
  394.      * Expected errors are always returned with error mode
  395.      * PEAR_ERROR_RETURN.  Expected error codes are stored in a stack,
  396.      * and this method pushes a new element onto it.  The list of
  397.      * expected errors are in effect until they are popped off the
  398.      * stack with the popExpect() method.
  399.      *
  400.      * Note that this method can not be called statically
  401.      *
  402.      * @param mixed $code a single error code or an array of error codes to expect
  403.      *
  404.      * @return int     the new depth of the "expected errors" stack
  405.      * @access public
  406.      */
  407.      /**
  408.       * 解读:PEAR解构方法
  409.       *
  410.       * 1 将类属性"$this->_expected_errors"作为栈,将错误编码压入栈,该栈是一个二维数组。
  411.       * 2 返回数组栈总项目数
  412.       *
  413.       ***************************************************************/
  414.     function expectError($code = '*')
  415.     {
  416.         if (is_array($code)) {
  417.             array_push($this->_expected_errors, $code);
  418.         } else {
  419.             array_push($this->_expected_errors, array($code));
  420.         }
  421.         return sizeof($this->_expected_errors);
  422.     }
  423.     // }}}
  424.     // {{{ popExpect()
  425.     /**
  426.      * This method pops one element off the expected error codes
  427.      * stack.
  428.      *
  429.      * @return array   the list of error codes that were popped
  430.      */
  431.     function popExpect()
  432.     {
  433.         return array_pop($this->_expected_errors);
  434.     }
  435.     // }}}
  436.     // {{{ _checkDelExpect()
  437.     /**
  438.      * This method checks unsets an error code if available
  439.      *
  440.      * @param mixed error code
  441.      * @return bool true if the error code was unset, false otherwise
  442.      * @access private
  443.      * @since PHP 4.3.0
  444.      */
  445.     function _checkDelExpect($error_code)
  446.     {
  447.         $deleted = false;
  448.         foreach ($this->_expected_errors AS $key => $error_array) {
  449.             if (in_array($error_code$error_array)) {
  450.                 unset($this->_expected_errors[$key][array_search($error_code$error_array)]);
  451.                 $deleted = true;
  452.             }
  453.             // clean up empty arrays
  454.             if (0 == count($this->_expected_errors[$key])) {
  455.                 unset($this->_expected_errors[$key]);
  456.             }
  457.         }
  458.         return $deleted;
  459.     }
  460.     // }}}
  461.     // {{{ delExpect()
  462.     /**
  463.      * This method deletes all occurences of the specified element from
  464.      * the expected error codes stack.
  465.      *
  466.      * @param  mixed $error_code error code that should be deleted
  467.      * @return mixed list of error codes that were deleted or error
  468.      * @access public
  469.      * @since PHP 4.3.0
  470.      */
  471.     function delExpect($error_code)
  472.     {
  473.         $deleted = false;
  474.         if ((is_array($error_code) && (0 != count($error_code)))) {
  475.             // $error_code is a non-empty array here;
  476.             // we walk through it trying to unset all
  477.             // values
  478.             foreach($error_code as $key => $error) {
  479.                 if ($this->_checkDelExpect($error)) {
  480.                     $deleted =  true;
  481.                 } else {
  482.                     $deleted = false;
  483.                 }
  484.             }
  485.             return $deleted ? true : PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME
  486.         } elseif (!emptyempty($error_code)) {
  487.             // $error_code comes alone, trying to unset it
  488.             if ($this->_checkDelExpect($error_code)) {
  489.                 return true;
  490.             } else {
  491.                 return PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME
  492.             }
  493.         } else {
  494.             // $error_code is empty
  495.             return PEAR::raiseError("The expected error you submitted is empty"); // IMPROVE ME
  496.         }
  497.     }
  498.     // }}}
  499.     // {{{ raiseError()
  500.     /**
  501.      * This method is a wrapper that returns an instance of the
  502.      * configured error class with this object's default error
  503.      * handling applied.  If the $mode and $options parameters are not
  504.      * specified, the object's defaults are used.
  505.      *
  506.      * @param mixed $message a text error message or a PEAR error object
  507.      *
  508.      * @param int $code      a numeric error code (it is up to your class
  509.      *                  to define these if you want to use codes)
  510.      *
  511.      * @param int $mode      One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT,
  512.      *                  PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE,
  513.      *                  PEAR_ERROR_CALLBACK, PEAR_ERROR_EXCEPTION.
  514.      *
  515.      * @param mixed $options If $mode is PEAR_ERROR_TRIGGER, this parameter
  516.      *                  specifies the PHP-internal error level (one of
  517.      *                  E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR).
  518.      *                  If $mode is PEAR_ERROR_CALLBACK, this
  519.      *                  parameter specifies the callback function or
  520.      *                  method.  In other error modes this parameter
  521.      *                  is ignored.
  522.      *
  523.      * @param string $userinfo If you need to pass along for example debug
  524.      *                  information, this parameter is meant for that.
  525.      *
  526.      * @param string $error_class The returned error object will be
  527.      *                  instantiated from this class, if specified.
  528.      *
  529.      * @param bool $skipmsg If true, raiseError will only pass error codes,
  530.      *                  the error message parameter will be dropped.
  531.      *
  532.      * @access public
  533.      * @return object   a PEAR error object
  534.      * @see PEAR::setErrorHandling
  535.      * @since PHP 4.0.5
  536.      */
  537.     function &raiseError($message = null,
  538.                          $code = null,
  539.                          $mode = null,
  540.                          $options = null,
  541.                          $userinfo = null,
  542.                          $error_class = null,
  543.                          $skipmsg = false)
  544.     {
  545.         // The error is yet a PEAR error object
  546.         if (is_object($message)) {
  547.             $code        = $message->getCode();
  548.             $userinfo    = $message->getUserInfo();
  549.             $error_class = $message->getType();
  550.             $message->error_message_prefix = '';
  551.             $message     = $message->getMessage();
  552.         }
  553.         if (isset($this) && isset($this->_expected_errors) && sizeof($this->_expected_errors) > 0 && sizeof($exp = end($this->_expected_errors))) {
  554.             if ($exp[0] == "*" ||
  555.                 (is_int(reset($exp)) && in_array($code$exp)) ||
  556.                 (is_string(reset($exp)) && in_array($message$exp))) {
  557.                 $mode = PEAR_ERROR_RETURN;
  558.             }
  559.         }
  560.         // No mode given, try global ones
  561.         if ($mode === null) {
  562.             // Class error handler
  563.             if (isset($this) && isset($this->_default_error_mode)) {
  564.                 $mode    = $this->_default_error_mode;
  565.                 $options = $this->_default_error_options;
  566.             // Global error handler
  567.             } elseif (isset($GLOBALS['_PEAR_default_error_mode'])) {
  568.                 $mode    = $GLOBALS['_PEAR_default_error_mode'];
  569.                 $options = $GLOBALS['_PEAR_default_error_options'];
  570.             }
  571.         }
  572.         if ($error_class !== null) {
  573.             $ec = $error_class;
  574.         } elseif (isset($this) && isset($this->_error_class)) {
  575.             $ec = $this->_error_class;
  576.         } else {
  577.             $ec = 'PEAR_Error';
  578.         }
  579.         if ($skipmsg) {
  580.             $a = new $ec($code$mode$options$userinfo);
  581.             return $a;
  582.         } else {
  583.             $a = new $ec($message$code$mode$options$userinfo);
  584.             return $a;
  585.         }
  586.     }
  587.     // }}}
  588.     // {{{ throwError()
  589.     /**
  590.      * Simpler form of raiseError with fewer options.  In most cases
  591.      * message, code and userinfo are enough.
  592.      *
  593.      * @param string $message
  594.      *
  595.      */
  596.     function &throwError($message = null,
  597.                          $code = null,
  598.                          $userinfo = null)
  599.     {
  600.         if (isset($this) && is_a($this'PEAR')) {
  601.             $a = $this->raiseError($message$code, null, null, $userinfo);
  602.             return $a;
  603.         } else {
  604.             $a = &PEAR::raiseError($message$code, null, null, $userinfo);
  605.             return $a;
  606.         }
  607.     }
  608.     // }}}
  609.     function staticPushErrorHandling($mode$options = null)
  610.     {
  611.         $stack = $GLOBALS['_PEAR_error_handler_stack'];
  612.         $def_mode    = $GLOBALS['_PEAR_default_error_mode'];
  613.         $def_options = $GLOBALS['_PEAR_default_error_options'];
  614.         $stack[] = array($def_mode$def_options);
  615.         switch ($mode) {
  616.             case PEAR_ERROR_EXCEPTION:
  617.             case PEAR_ERROR_RETURN:
  618.             case PEAR_ERROR_PRINT:
  619.             case PEAR_ERROR_TRIGGER:
  620.             case PEAR_ERROR_DIE:
  621.             case null:
  622.                 $def_mode = $mode;
  623.                 $def_options = $options;
  624.                 break;
  625.             case PEAR_ERROR_CALLBACK:
  626.                 $def_mode = $mode;
  627.                 // class/object method callback
  628.                 if (is_callable($options)) {
  629.                     $def_options = $options;
  630.                 } else {
  631.                     trigger_error("invalid error callback", E_USER_WARNING);
  632.                 }
  633.                 break;
  634.             default:
  635.                 trigger_error("invalid error mode", E_USER_WARNING);
  636.                 break;
  637.         }
  638.         $stack[] = array($mode$options);
  639.         return true;
  640.     }
  641.     function staticPopErrorHandling()
  642.     {
  643.         $stack = $GLOBALS['_PEAR_error_handler_stack'];
  644.         $setmode     = $GLOBALS['_PEAR_default_error_mode'];
  645.         $setoptions  = $GLOBALS['_PEAR_default_error_options'];
  646.         array_pop($stack);
  647.         list($mode$options) = $stack[sizeof($stack) - 1];
  648.         array_pop($stack);
  649.         switch ($mode) {
  650.             case PEAR_ERROR_EXCEPTION:
  651.             case PEAR_ERROR_RETURN:
  652.             case PEAR_ERROR_PRINT:
  653.             case PEAR_ERROR_TRIGGER:
  654.             case PEAR_ERROR_DIE:
  655.             case null:
  656.                 $setmode = $mode;
  657.                 $setoptions = $options;
  658.                 break;
  659.             case PEAR_ERROR_CALLBACK:
  660.                 $setmode = $mode;
  661.                 // class/object method callback
  662.                 if (is_callable($options)) {
  663.                     $setoptions = $options;
  664.                 } else {
  665.                     trigger_error("invalid error callback", E_USER_WARNING);
  666.                 }
  667.                 break;
  668.             default:
  669.                 trigger_error("invalid error mode", E_USER_WARNING);
  670.                 break;
  671.         }
  672.         return true;
  673.     }
  674.     // {{{ pushErrorHandling()
  675.     /**
  676.      * Push a new error handler on top of the error handler options stack. With this
  677.      * you can easily override the actual error handler for some code and restore
  678.      * it later with popErrorHandling.
  679.      *
  680.      * @param mixed $mode (same as setErrorHandling)
  681.      * @param mixed $options (same as setErrorHandling)
  682.      *
  683.      * @return bool Always true
  684.      *
  685.      * @see PEAR::setErrorHandling
  686.      */
  687.     function pushErrorHandling($mode$options = null)
  688.     {
  689.         $stack = $GLOBALS['_PEAR_error_handler_stack'];
  690.         if (isset($this) && is_a($this'PEAR')) {
  691.             $def_mode    = $this->_default_error_mode;
  692.             $def_options = $this->_default_error_options;
  693.         } else {
  694.             $def_mode    = $GLOBALS['_PEAR_default_error_mode'];
  695.             $def_options = $GLOBALS['_PEAR_default_error_options'];
  696.         }
  697.         $stack[] = array($def_mode$def_options);
  698.         if (isset($this) && is_a($this'PEAR')) {
  699.             $this->setErrorHandling($mode$options);
  700.         } else {
  701.             PEAR::setErrorHandling($mode$options);
  702.         }
  703.         $stack[] = array($mode$options);
  704.         return true;
  705.     }
  706.     // }}}
  707.     // {{{ popErrorHandling()
  708.     /**
  709.     * Pop the last error handler used
  710.     *
  711.     * @return bool Always true
  712.     *
  713.     * @see PEAR::pushErrorHandling
  714.     */
  715.     function popErrorHandling()
  716.     {
  717.         $stack = $GLOBALS['_PEAR_error_handler_stack'];
  718.         array_pop($stack);
  719.         list($mode$options) = $stack[sizeof($stack) - 1];
  720.         array_pop($stack);
  721.         if (isset($this) && is_a($this'PEAR')) {
  722.             $this->setErrorHandling($mode$options);
  723.         } else {
  724.             PEAR::setErrorHandling($mode$options);
  725.         }
  726.         return true;
  727.     }
  728.     // }}}
  729.     // {{{ loadExtension()
  730.     /**
  731.     * OS independant PHP extension load. Remember to take care
  732.     * on the correct extension name for case sensitive OSes.
  733.     *
  734.     * @param string $ext The extension name
  735.     * @return bool Success or not on the dl() call
  736.     */
  737.     function loadExtension($ext)
  738.     {
  739.         if (!extension_loaded($ext)) {
  740.             // if either returns true dl() will produce a FATAL error, stop that
  741.             if ((ini_get('enable_dl') != 1) || (ini_get('safe_mode') == 1)) {
  742.                 return false;
  743.             }
  744.             if (OS_WINDOWS) {
  745.                 $suffix = '.dll';
  746.             } elseif (PHP_OS == 'HP-UX') {
  747.                 $suffix = '.sl';
  748.             } elseif (PHP_OS == 'AIX') {
  749.                 $suffix = '.a';
  750.             } elseif (PHP_OS == 'OSX') {
  751.                 $suffix = '.bundle';
  752.             } else {
  753.                 $suffix = '.so';
  754.             }
  755.             return @dl('php_'.$ext.$suffix) || @dl($ext.$suffix);
  756.         }
  757.         return true;
  758.     }
  759.     // }}}
  760. }
  761. // {{{ _PEAR_call_destructors()
  762.     /**
  763.       * 解读:PEAR脚本关闭执行函数
  764.       *
  765.       * 1 PEAR注册的脚本关闭时执行的函数就是该函数,该函数会依次执行已注册的脚本关闭函数对象列表的所有脚本关闭函数.
  766.       * 2 
  767.       *
  768.       ***************************************************************/
  769. function _PEAR_call_destructors()
  770. {
  771.     global $_PEAR_destructor_object_list;
  772.     if (is_array($_PEAR_destructor_object_list) &
  773.         sizeof($_PEAR_destructor_object_list))
  774.     {
  775.         reset($_PEAR_destructor_object_list);
  776.         if (PEAR::getStaticProperty('PEAR''destructlifo')) {
  777.             $_PEAR_destructor_object_list = array_reverse($_PEAR_destructor_object_list);
  778.         }
  779.         while (list($k$objref) = each($_PEAR_destructor_object_list)) {
  780.             $classname = get_class($objref);
  781.             while ($classname) {
  782.                 $destructor = "_$classname";
  783.                 if (method_exists($objref$destructor)) {
  784.                     $objref->$destructor();
  785.                     break;
  786.                 } else {
  787.                     $classname = get_parent_class($classname);
  788.                 }
  789.             }
  790.         }
  791.         // Empty the object list to ensure that destructors are
  792.         // not called more than once.
  793.         $_PEAR_destructor_object_list = array();
  794.     }
  795.     // Now call the shutdown functions
  796.     if (is_array($GLOBALS['_PEAR_shutdown_funcs']) AND !emptyempty($GLOBALS['_PEAR_shutdown_funcs'])) {
  797.         foreach ($GLOBALS['_PEAR_shutdown_funcs'as $value) {
  798.             call_user_func_array($value[0], $value[1]);
  799.         }
  800.     }
  801. }
  802. // }}}
  803. /**
  804.  * Standard PEAR error class for PHP 4
  805.  *
  806.  * This class is supserseded by {@link PEAR_Exception} in PHP 5
  807.  *
  808.  * @category   pear
  809.  * @package    PEAR
  810.  * @author     Stig Bakken <ssb@php.net>
  811.  * @author     Tomas V.V. Cox <cox@idecnet.com>
  812.  * @author     Gregory Beaver <cellog@php.net>
  813.  * @copyright  1997-2006 The PHP Group
  814.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  815.  * @version    Release: 1.6.2
  816.  * @link       http://pear.php.net/manual/en/core.pear.pear-error.php
  817.  * @see        PEAR::raiseError(), PEAR::throwError()
  818.  * @since      Class available since PHP 4.0.2
  819.  */
  820. class PEAR_Error
  821. {
  822.     // {{{ properties
  823.     var $error_message_prefix = '';
  824.     var $mode                 = PEAR_ERROR_RETURN;
  825.     var $level                = E_USER_NOTICE;
  826.     var $code                 = -1;
  827.     var $message              = '';
  828.     var $userinfo             = '';
  829.     var $backtrace            = null;
  830.     // }}}
  831.     // {{{ constructor
  832.     /**
  833.      * PEAR_Error constructor
  834.      *
  835.      * @param string $message  message
  836.      *
  837.      * @param int $code     (optional) error code
  838.      *
  839.      * @param int $mode     (optional) error mode, one of: PEAR_ERROR_RETURN,
  840.      * PEAR_ERROR_PRINT, PEAR_ERROR_DIE, PEAR_ERROR_TRIGGER,
  841.      * PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION
  842.      *
  843.      * @param mixed $options   (optional) error level, _OR_ in the case of
  844.      * PEAR_ERROR_CALLBACK, the callback function or object/method
  845.      * tuple.
  846.      *
  847.      * @param string $userinfo (optional) additional user/debug info
  848.      *
  849.      * @access public
  850.      *
  851.      */
  852.      /**
  853.       * 解读:PEAR_Error类构造方法
  854.       *
  855.       * 1 构造参数"$code" 是一个自定义的错误编码,可以是任何整数.
  856.       * 2 如果没有设置构造参数->错误模式"$mode",系统默认为返回错误消息模式"PEAR_ERROR_RETURN",错误模式是必须的 .
  857.       * 3 将构造参数的值直接赋值到PEAR_Error类属性,不经过任何处理 .
  858.       * 4 如果错误模式是"PEAR_ERROR_CALLBACK",设置系统错误级别为"E_USER_NOTICE",构造参数"$options"设置为回调函数.
  859.       *   如果错误级别不是"PEAR_ERROR_CALLBACK",这时候构造参数"$options"的值为一个整数,用来表示PHP 系统错误级别,如
  860.       *   果这个时候没有设置"$option",那么"$option",将会被自动设置为"E_USER_NOTICE".并且将构造参数的当前值赋值到
  861.       *   EPAR_Error类属性中.
  862.       * 5 这里有一个问题,如果PEAR系统错误模式是"PEAR_ERROR_CALLBACK",那么构造参数"$options"就是回调函数的名字了,如果不
  863.       *   是这个错误模式,那么构造参数"$options"的值就是一个整数表示PHP系统错误级别,并且如果没有设置,会被系统默认设置为
  864.       *   "E_USER_NOTICE"级别 .
  865.       * 6 错误模式的判断从高到底依次判断,高低排序按照错误模式所表示的整数大小为依据.
  866.       * 7 如果错误模式是打印错误,那么直接输出错误信息.该信息通过"$this->getMessage()"获得.在这个地方构造参数"$options"
  867.       *   又可以作为用在函数"printf"的输出信息格式,由此看来构造参数"$options"是一个很灵活的参数,这个得益于PHP灵活的变量类型.
  868.       * 8 如果错误模式是出发PHP系统错误,那么执行触发错误函数,错误级别为构造参数"$options"设置的值.说明,在这个错误模式中,构造
  869.       *   参数"$options"的值,只能是系统定义的PHP错误级别的任意一个.
  870.       * 9 如果是停止脚本运行模式"PEAR_ERROR_DIE",系统会通过DIE函数输出错误信息,并停止脚本运行.如果错误消息中最后一个字符不是
  871.       *   "/n" 那么会自动添加"/n".构造参数"$option"依然负责输出字符模式,不过这次用在函数"sprintf"中,该函数"sprintf"返回经
  872.       *   过模式处理的字符串,这个与"printf"直接输出有所不同.
  873.       *10 如果错误模式是"PEAR_ERROR_CALLBACK",会执行已注册的回调函数,并将当前对象作为唯一参数传递.
  874.       *11 如果错误模式是"PEAR_ERROR_EXCEPTION"异常处理模式,那么PEAR_error会通过异常处理的方式来处理这个错误,并抛出异常信息.
  875.       *
  876.       ***************************************************************/
  877.     function PEAR_Error($message = 'unknown error'$code = null,
  878.                         $mode = null, $options = null, $userinfo = null)
  879.     {
  880.         if ($mode === null) {
  881.             $mode = PEAR_ERROR_RETURN;
  882.         }
  883.         $this->message   = $message;
  884.         $this->code      = $code;
  885.         $this->mode      = $mode;
  886.         $this->userinfo  = $userinfo;
  887.         if (!PEAR::getStaticProperty('PEAR_Error''skiptrace')) {
  888.             $this->backtrace = debug_backtrace();
  889.             if (isset($this->backtrace[0]) && isset($this->backtrace[0]['object'])) {
  890.                 unset($this->backtrace[0]['object']);
  891.             }
  892.         }
  893.         if ($mode & PEAR_ERROR_CALLBACK) {
  894.             $this->level = E_USER_NOTICE;
  895.             $this->callback = $options;
  896.         } else {
  897.             if ($options === null) {
  898.                 $options = E_USER_NOTICE;
  899.             }
  900.             $this->level = $options;
  901.             $this->callback = null;
  902.         }
  903.         if ($this->mode & PEAR_ERROR_PRINT) {
  904.             if (is_null($options) || is_int($options)) {
  905.                 $format = "%s";
  906.             } else {
  907.                 $format = $options;
  908.             }
  909.             printf($format$this->getMessage());
  910.         }
  911.         if ($this->mode & PEAR_ERROR_TRIGGER) {
  912.             trigger_error($this->getMessage(), $this->level);
  913.         }
  914.         if ($this->mode & PEAR_ERROR_DIE) {
  915.             $msg = $this->getMessage();
  916.             if (is_null($options) || is_int($options)) {
  917.                 $format = "%s";
  918.                 if (substr($msg, -1) != "/n") {
  919.                     $msg .= "/n";
  920.                 }
  921.             } else {
  922.                 $format = $options;
  923.             }
  924.             die(sprintf($format$msg));
  925.         }
  926.         if ($this->mode & PEAR_ERROR_CALLBACK) {
  927.             if (is_callable($this->callback)) {
  928.                 call_user_func($this->callback, $this);
  929.             }
  930.         }
  931.         if ($this->mode & PEAR_ERROR_EXCEPTION) {
  932.             trigger_error("PEAR_ERROR_EXCEPTION is obsolete, use class PEAR_Exception for exceptions", E_USER_WARNING);
  933.             eval('$e = new Exception($this->message, $this->code);throw($e);');
  934.         }
  935.     }
  936.     // }}}
  937.     // {{{ getMode()
  938.     /**
  939.      * Get the error mode from an error object.
  940.      *
  941.      * @return int error mode
  942.      * @access public
  943.      */
  944.     function getMode() {
  945.         return $this->mode;
  946.     }
  947.     // }}}
  948.     // {{{ getCallback()
  949.     /**
  950.      * Get the callback function/method from an error object.
  951.      *
  952.      * @return mixed callback function or object/method array
  953.      * @access public
  954.      */
  955.     function getCallback() {
  956.         return $this->callback;
  957.     }
  958.     // }}}
  959.     // {{{ getMessage()
  960.     /**
  961.      * Get the error message from an error object.
  962.      *
  963.      * @return  string  full error message
  964.      * @access public
  965.      */
  966.     function getMessage()
  967.     {
  968.         return ($this->error_message_prefix . $this->message);
  969.     }
  970.     // }}}
  971.     // {{{ getCode()
  972.     /**
  973.      * Get error code from an error object
  974.      *
  975.      * @return int error code
  976.      * @access public
  977.      */
  978.      function getCode()
  979.      {
  980.         return $this->code;
  981.      }
  982.     // }}}
  983.     // {{{ getType()
  984.     /**
  985.      * Get the name of this error/exception.
  986.      *
  987.      * @return string error/exception name (type)
  988.      * @access public
  989.      */
  990.     function getType()
  991.     {
  992.         return get_class($this);
  993.     }
  994.     // }}}
  995.     // {{{ getUserInfo()
  996.     /**
  997.      * Get additional user-supplied information.
  998.      *
  999.      * @return string user-supplied information
  1000.      * @access public
  1001.      */
  1002.     function getUserInfo()
  1003.     {
  1004.         return $this->userinfo;
  1005.     }
  1006.     // }}}
  1007.     // {{{ getDebugInfo()
  1008.     /**
  1009.      * Get additional debug information supplied by the application.
  1010.      *
  1011.      * @return string debug information
  1012.      * @access public
  1013.      */
  1014.     function getDebugInfo()
  1015.     {
  1016.         return $this->getUserInfo();
  1017.     }
  1018.     // }}}
  1019.     // {{{ getBacktrace()
  1020.     /**
  1021.      * Get the call backtrace from where the error was generated.
  1022.      * Supported with PHP 4.3.0 or newer.
  1023.      *
  1024.      * @param int $frame (optional) what frame to fetch
  1025.      * @return array Backtrace, or NULL if not available.
  1026.      * @access public
  1027.      */
  1028.     function getBacktrace($frame = null)
  1029.     {
  1030.         if (defined('PEAR_IGNORE_BACKTRACE')) {
  1031.             return null;
  1032.         }
  1033.         if ($frame === null) {
  1034.             return $this->backtrace;
  1035.         }
  1036.         return $this->backtrace[$frame];
  1037.     }
  1038.     // }}}
  1039.     // {{{ addUserInfo()
  1040.     function addUserInfo($info)
  1041.     {
  1042.         if (emptyempty($this->userinfo)) {
  1043.             $this->userinfo = $info;
  1044.         } else {
  1045.             $this->userinfo .= " ** $info";
  1046.         }
  1047.     }
  1048.     // }}}
  1049.     // {{{ toString()
  1050.     /**
  1051.      * Make a string representation of this object.
  1052.      *
  1053.      * @return string a string with an object summary
  1054.      * @access public
  1055.      */
  1056.     function toString() {
  1057.         $modes = array();
  1058.         $levels = array(E_USER_NOTICE  => 'notice',
  1059.                         E_USER_WARNING => 'warning',
  1060.                         E_USER_ERROR   => 'error');
  1061.         if ($this->mode & PEAR_ERROR_CALLBACK) {
  1062.             if (is_array($this->callback)) {
  1063.                 $callback = (is_object($this->callback[0]) ?
  1064.                     strtolower(get_class($this->callback[0])) :
  1065.                     $this->callback[0]) . '::' .
  1066.                     $this->callback[1];
  1067.             } else {
  1068.                 $callback = $this->callback;
  1069.             }
  1070.             return sprintf('[%s: message="%s" code=%d mode=callback '.
  1071.                            'callback=%s prefix="%s" info="%s"]',
  1072.                            strtolower(get_class($this)), $this->message, $this->code,
  1073.                            $callback$this->error_message_prefix,
  1074.                            $this->userinfo);
  1075.         }
  1076.         if ($this->mode & PEAR_ERROR_PRINT) {
  1077.             $modes[] = 'print';
  1078.         }
  1079.         if ($this->mode & PEAR_ERROR_TRIGGER) {
  1080.             $modes[] = 'trigger';
  1081.         }
  1082.         if ($this->mode & PEAR_ERROR_DIE) {
  1083.             $modes[] = 'die';
  1084.         }
  1085.         if ($this->mode & PEAR_ERROR_RETURN) {
  1086.             $modes[] = 'return';
  1087.         }
  1088.         return sprintf('[%s: message="%s" code=%d mode=%s level=%s '.
  1089.                        'prefix="%s" info="%s"]',
  1090.                        strtolower(get_class($this)), $this->message, $this->code,
  1091.                        implode("|"$modes), $levels[$this->level],
  1092.                        $this->error_message_prefix,
  1093.                        $this->userinfo);
  1094.     }
  1095.     // }}}
  1096. }
  1097. /*
  1098.  * Local Variables:
  1099.  * mode: php
  1100.  * tab-width: 4
  1101.  * c-basic-offset: 4
  1102.  * End:
  1103.  */
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 系统重装卡住了怎么办 系统关机没反应怎么办 232串口打开失败怎么办 逆水寒cpu不支持怎么办 显卡被禁用了怎么办 vmvare注册错了怎么办 电脑主机未成功启动怎么办 主机未成功启动怎么办 电脑主机未能成功启动怎么办 虚拟机没有自带怎么办 错误连接为720怎么办 dns错误不能上网怎么办 家里无线用不了怎么办 磁盘c5坏了怎么办 一体机装xp蓝屏怎么办 虚拟机密码忘记了怎么办 vivoy66手机太卡怎么办 虚拟机装xp蓝屏怎么办 exagear玩起来卡怎么办 第五人格模拟器玩太卡怎么办 速腾油箱盖打不开怎么办 奥迪a6油箱盖打不开怎么办 苹果手提虚拟机黑屏怎么办 mac系统桌面变大怎么办 删除文件要权限怎么办 页面载入错误了怎么办 手机打不开excel表格怎么办 皇室战争闪退怎么办 苹果老是闪退怎么办 黑苹果开机黑屏怎么办 MAC磁盘删了怎么办 mac磁盘被锁定怎么办 bt5读不到网卡怎么办 笔记本电脑cpu温度过高怎么办 笔记本cpu温度过高怎么办 联想系统崩溃了怎么办 办公软件用不了怎么办 win10设置闪退怎么办 手机浏览器版本低怎么办 wps界面动不了怎么办 手机设置删了怎么办