PHP Classes
elePHPant
Icontem

File: example_errorhandling.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Kalle Sommer Nielsen  >  Template Processor  >  example_errorhandling.php  >  Download  
File: example_errorhandling.php
Role: Example script
Content type: text/plain
Description: Example file - Using error handling functions
Class: Template Processor
Template processing engine
Author: By
Last change: Updated to match latest class version
Date: 8 years ago
Size: 1,622 bytes
 

Contents

Class file image Download
<?php
   
/**
     * New error handler introduced in v1.0.2, notice the new class name as of v1.0.2 also
     */

    /**
     * Declare a class with an error function
     */
   
class ErrorHandler
   
{
        function
ErrorHandler($message)
        {
            die(
'<h3>Template Error from class method</h3><pre>' . $message . '</pre>');
        }
    }

   
/**
     * Declare an error function
     */
   
function ErrorHandler($message)
    {
        die(
'<h3>Template Error from function</h3><pre>' . $message . '</pre>');
    }

   
/**
     * Load template class
     */
   
require('./template.class.php');

   
/**
     * Init template class
     */
   
$template = new Template(Array('ext' => 'php'));

   
/**
     * Set error handler method to the function reference
     */
   
$template->set_error_handler(Array('type' => 'function', 'function' => 'ErrorHandler'));

   
/**
     * Generate an error
     */
   
$template->set_option('foo', 'bar');

   
/**
     * This will print something like:
     *
     * <h3>Template Error from function</h3>
     * <pre>Trying to define a not allowed option!</pre>
     *
     */

    /**
     * Now we want to use the class with an error function aka. method to handle our error
     */

    /**
     * Set error handler method to the class reference
     */
   
$template->set_error_handler('type' => 'object', 'object' => 'ErrorHandler', 'function' => 'ErrorHandler'));

   
/**
     * By making a new error again...
     */
   
$template->set_option('bar', 'foo');

   
/**
     * ... will cause the error handler to be triggered and generate this output:
     *
     * <h3>Template Error from class method</h3>
     * <pre>Trying to define a not allowed option!</pre>
     *
     */
?>