PhP5 exception handling mechanism [2]-die () of error handling before PhP5 ()

Source: Internet
Author: User
Tags stop script

Handle errors before PhP5

The following three methods are used to handle program errors before PhP5:
1. Use the trigger_error () or die () function to generate a warning or fatal error (fatal error) at the script level );
2. return an error mark (such as false) in a class method or function, or set an attribute or global variable (such as $ error) that can be checked later ), check its value in the appropriate place and decide whether to continue executing the program (for example, if ($ error = 1 ){});
3. Use pear to handle errors;

(1) Use die () or trigger_error ()

You can use the die () function to end the program running. The following is a simple class that tries to load a class file from a directory.

Index. php

// PHP 4
require_once('cmd_php4/Command.php');
class CommandManager {
    var $cmdDir = "cmd_php4";

    function getCommandObject($cmd) {
        $path = "{$this->cmdDir}/{$cmd}.php";
        if (!file_exists($path)) {
            die("Cannot find $path/n");
        }
        require_once
$path;

        if (!class_exists($cmd)) {
            die("class $cmd does not exist");
        }

        $ret = new $cmd();
        if (!is_a($ret, 'Command')) {
            die("$cmd is not a Command");
        }
        return
$ret;
    }
}
?>

This is a simple example of implementing the "command Pattern Design Pattern" using PHP (see Java and pattern). Programmers who use this class (client CODER) can put a class in the directory (in this example, the cmd_php4 directory ). Once the file has the same name as the class contained in it and the class is a subclass of the command class, our class method will generate an available command object. The command class defines an execute () method used to execute the found command, that is, the object returned by the getcommandobject () method will execute (). let's take a look at the parent class command class, and we will store it into pai_php4/command. PHP file.

Pai_php4/command. php


// PHP 4
class Command {
    function execute() {
        die("Command::execute() is an abstract method");
    }
}
?>

 

As you can see, the command is the implementation of the abstract class in PhP4, and we cannot instantiate it directly. Instead, we must first assign sub-classes from the middle class and then instantiate them. When we use PhP5, we can use a better method-use the abstract keyword to declare classes and methods as "abstract ":


// PHP 5
abstract class Command {
    abstract function execute();
}
?>

 

The following is the implementation of the preceding abstract class, overwriting the execute () method, and adding the truly executable content to it. This class is named realcommand, which can be found in the pai_php4/realcommand. php file.

Pai_php4/realcommand. php


// PHP 4
require_once 'Command.php';
class realcommand extends Command {
    function execute() {
        print "realcommand::execute() executing as ordered sah!/n";
    }
}
?>

 

Using this structure can make the code flexible. You can add a new command class at any time without changing the peripheral framework. But you have to pay attention to some potential factors that stop script execution. Make sure that the class file exists and the class exists in the file, and the class is a sub-class of command> (just like realcommand ).

In this example, if we try to find the class operation fails, the script execution will be aborted, which reflects the code security. However, this code is not flexible and not flexible enough. The extreme reflection is that class methods can only perform positive operations. They are only responsible for finding and instantiating a command object. It cannot handle errors executed by scripts in a larger scope (of course, it should not be responsible for handling errors. If we add too many links to the surrounding code to a class method, so the reuse of this class will become difficult and difficult to expand ). Although the use of die () avoids the risk of embedding script logic in the getcommandobject () method, it is too responsive to errors-stop the program immediately. In fact, sometimes we do not want to immediately stop executing the program when we cannot find the desired class file. Maybe we have a default command for the program to continue executing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.