Location: PHPKode > scripts > PHS_Error > phs_error/phs_error.inc.php
<?
//! \brief Class file. 
//! \author Andy (cuigri [at] yahoo [dot] com)
//! \version 1.17
//! \example example.php

//! Set current version of class
PHS_Error::class_info( "CLASS_VERSION", "1.17" );
//! Author
PHS_Error::class_info( "CLASS_AUTHOR", "Andy" );
//! Contact
PHS_Error::class_info( "CLASS_CONTACT", "cuigri [at] yahoo [dot] com" );
//! Class description
PHS_Error::class_info( "CLASS_DESCRIPTION", "Enhanced debugging." );

//! Enhanced debugging.
class PHS_Error
{
    //! Error string
    var $error_msg;
    //! Error code
    var $error_no;

    //! Default error code (usually 0). This should be "no error" code
    var $def_no;
    //! Default error message (eg. "Class inited")
    var $def_msg;

    //! Warnings array (it supports providing a "tag" for specific warnings so they can be deleted if needed)
    var $warnings;
    //! Number of warnings
    var $warnings_no;

    //! Class constructor
    /**
     *  \param $def_no (int) Default error code
     *  \param $def_msg (string) Default error message
     */
    function PHS_Error( $def_no = 0, $def_msg = "" )
    {
         $this->error_no = $def_no;
         $this->error_msg = $def_msg;
         
         $this->def_no = $def_no;
         $this->def_msg = $def_msg;
         
         $this->warnings = false;
         $this->warnings_no = 0;
    }

    //! Tells if we have an error
     /**
      *   Tells if current error is different than default error code provided in constructor meaning there is an error.
      *
      *   \return (bool) True if there is an error, false if no error
      **/
    function has_error()
    {
        return ($this->error_no != $this->def_no);
    }

    //! Get number of warnings
     /**
      *   Method returns number of warnings warnings (for specified tag or as total)
      *
      *   \param $tag Check if we have warnings for provided tag (false by default)
      *   \return (int) Return warnings number (for specified tag or as total)
      **/
    function has_warnings( $tag = false )
    {
        if( $tag === false )
            return $this->warnings_no;
        elseif( isset( $this->warnings[$tag] ) and is_array( $this->warnings[$tag] ) )
            return count( $this->warnings[$tag] );
        else
            return 0;
    }

    //! Set error code and error message
     /**
      *   Set an error code and error message. Also method will make a backtrace of this call and present all functions/methods called (with their parameters) and files/line of call.
      *
      *   \param $error_no (int) Error code
      *   \param $error_msg (string) Error message
      **/
    function set_error( $error_no, $error_msg )
    {
         if( is_array( ($err_info = debug_backtrace()) ) )
         {
             $lvl = count( $err_info );
             for( $i = $lvl-1; $i >= 0; $i-- )
             {
                 $args_str = "";
                 if( is_array( $err_info[$i]["args"] ) )
                 {
                     foreach( $err_info[$i]["args"] as $key => $val )
                     {
                         if( is_bool( $val ) )
                             $args_str .= "(".gettype( $val ).") [".($val?"true":"false")."], ";
                         elseif( is_resource( $val ) )
                             $args_str .= "("hide@address.com( $val )."), ";
                         elseif( !is_object( $val ) )
                             $args_str .= "(".gettype( $val ).") [".$val."], ";
                         else
                             $args_str .= "("hide@address.com( $val )."), ";
                     }
                     
                     $args_str = substr( $args_str, 0, -2 );
                 } else
                     $args_str = $err_info[$i]["args"];

                 $backtrace .= "#".($lvl-$i).". ".$err_info[$i]["class"].$err_info[$i]["type"].$err_info[$i]["function"]."( ".$args_str." ) -> ".
                               $err_info[$i]["file"].":".$err_info[$i]["line"]."\n";
             }
         }

         $this->error_no = $error_no;
         $this->error_msg = "Error: (".$error_msg.")\n".
                            "Code: (".$error_no.")\n".
                            "Backtrace:\n".
                            $backtrace;
    }

    //! Add a warning message
     /**
      *   Add a warning message for a speficied tag or as general warning. Also method will make a backtrace of this call and present all functions/methods called (with their parameters) and files/line of call.
      *
      *   \param $warning (string) Warning message
      *   \param $tag (string) Add warning for a specific tag (default false). If this is not provided warning will be added as general warning.
      **/
    function add_warning( $warning, $tag = false )
    {
         if( $this->warnings === false )
             $this->warnings = array();
         
         if( is_array( ($err_info = debug_backtrace()) ) )
         {
             $lvl = count( $err_info );
             for( $i = $lvl-1; $i >= 0; $i-- )
             {
                 $args_str = "";
                 if( is_array( $err_info[$i]["args"] ) )
                 {
                     foreach( $err_info[$i]["args"] as $key => $val )
                     {
                         if( is_bool( $val ) )
                             $args_str .= "(".gettype( $val ).") [".($val?"true":"false")."], ";
                         elseif( is_resource( $val ) )
                             $args_str .= "("hide@address.com( $val )."), ";
                         elseif( !is_object( $val ) )
                             $args_str .= "(".gettype( $val ).") [".$val."], ";
                         else
                             $args_str .= "(".get_class( $val )."), ";
                     }
                     
                     $args_str = substr( $args_str, 0, -2 );
                 } else
                     $args_str = $err_info[$i]["args"];

                 $backtrace .= "#".($lvl-$i).". ".$err_info[$i]["class"].$err_info[$i]["type"].$err_info[$i]["function"]."( ".$args_str." ) -> ".
                               $err_info[$i]["file"].":".$err_info[$i]["line"]."\n";
             }
         }

         if( !empty( $tag ) )
         {
             if( !isset( $this->warnings[$tag] ) )
                 $this->warnings[$tag] = array();

             $this->warnings[$tag][] = $warning."\n".
                                       "Backtrace:\n".
                                       $backtrace;
         } else
             $this->warnings[] = $warning."\n".
                                 "Backtrace:\n".
                                 $backtrace;

         $this->warnings_no++;
    }
    
    //! Remove warnings
     /**
      *   Remove warning messages for a speficied tag or all warnings.
      *
      *   \param $tag (string) Remove warnings of specific tag or all warnings. (default false)
      **/
    function reset_warnings( $tag = false )
    {
        if( $tag !== false )
        {
            if( isset( $this->warnings[$tag] ) and is_array( $this->warnings[$tag] ) )
            {
                $this->warnings_no -= count( $this->warnings[$tag] );
                unset( $this->warnings[$tag] );

                if( !$this->warnings_no )
                    $this->warnings = false;
            }
        } else
        {
            $this->warnings = false;
            $this->warnings_no = 0;
        }

        return $this->warnings_no;
    }

    //! Get error details
     /**
      *   Method returns an array with current error code and message.
      *
      *   \return (array) Array with indexes "error_no" for error code and "error_msg" for error message
      **/
    function get_error()
    {
        return array( "error_no" => $this->error_no, "error_msg" => $this->error_msg );
    }

    //! Return warnings for specified tag or all warnings
     /**
      *   Return warnings array for specified tag (if any) or 
      *
      *   \param $tag Check if we have warnings for provided tag (false by default)
      *   \return (mixed) Return array of warnings (all or for specified tag) or false if no warnings
      **/
    function get_warnings( $tag = false )
    {
        if( $this->warnings === false
         or ($tag !== false and !isset( $this->warnings[$tag] )) )
            return false;

        $ret_warnings = array();
        
        if( $tag === false )
            $warning_pool = $this->warnings;
        else
            $warning_pool = $this->warnings[$tag];

        foreach( $warning_pool as $wtag => $warning )
        {
            if( is_array( $warning ) )
            {
                foreach( $warning as $junk => $value )
                  $ret_warnings[] = "[".$wtag."] ".$value;
            } else
                $ret_warnings[] = $warning;
        }

        return $ret_warnings;
    }
    
    //! \brief Returns function/method call backtrace
    /**
      *  Used for debugging calls to functions or methods.
      *  \return Method will return a string representing function/method calls.
      */
    function debug_call_backtrace()
    {
         $backtrace = "";
         if( is_array( ($err_info = debug_backtrace()) ) )
         {
             $lvl = count( $err_info );
             for( $i = $lvl-1; $i >= 0; $i-- )
             {
                 $args_str = "";
                 if( is_array( $err_info[$i]["args"] ) )
                 {
                     foreach( $err_info[$i]["args"] as $key => $val )
                     {
                         if( is_bool( $val ) )
                             $args_str .= "(".gettype( $val ).") [".($val?"true":"false")."], ";
                         elseif( is_resource( $val ) )
                             $args_str .= "("hide@address.com( $val )."), ";
                         elseif( !is_object( $val ) )
                             $args_str .= "(".gettype( $val ).") [".$val."], ";
                         else
                             $args_str .= "(".get_class( $val )."), ";
                     }
                     
                     $args_str = substr( $args_str, 0, -2 );
                 } else
                     $args_str = $err_info[$i]["args"];

                 $backtrace .= "#".($lvl-$i).". ".$err_info[$i]["class"].$err_info[$i]["type"].$err_info[$i]["function"]."( ".$args_str." ) -> ".
                               $err_info[$i]["file"].":".$err_info[$i]["line"]."\r\n";
             }
         }

         return $backtrace;
    }

    //! Set/get class details
     /**
      *   This method is called statically to set/get details about class implementation.
      *
      *   \param $name (string) Class property name
      *   \param $value (mixed) Class property value
      *   \return (mixed) Returns array of class details if no parameter passed to class, Value of desired info if $name is provided or nothig if both parameters are passed
      **/
    function &class_info( $name = NULL, $value = NULL )
    {
        static $class_infos;

        if( !is_array( $class_infos ) )
        {
            $class_infos = array();
        }

        if( is_null( $name ) and is_null( $value ) )
        {
            return $class_infos;
        } elseif( !is_null( $name ) and is_null( $value ) )
        {
            return (isset( $class_infos[$name] )?$class_infos[$name]:NULL);
        } else
        {
            $class_infos[$name] = $value;
        }
    }
}
?>
Return current item: PHS_Error