PHP5 exception handling mechanism [2] - error handling Die () before PHP5

xiaoxiao2021-03-06  84

PHP5 previous error Processing Program Error Processing before PHP5 is used in three ways: 1. Use the Trigger_Error () or Die () function to generate a warning (WARNING) or fatal error (FATAL ERROR); 2. Returns an error tag (such as false) in a class method or function, or it is also possible to set an attribute or global variable (such as $ error) after one, and then verify the value of whether or not to continue executing the program (eg if IF ($ Error == 1) {}); 3. Using a PEAR handle; (1) Using DIE () or Trigger_Error () You can use the DIE () function to end the program run. The following is a simple class that attempts 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_On / 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 "Command Pattern Design Mode" with PHP (see "Java and Mode"). Programmers using this class (Customer CLIENT CODER) can place a class into the directory (CMD_PHP4 directory). Once the file and the class included in the class, and this class is a subclass of the Command class, our class method will generate an available Command object. A command () method is defined in the Command class to perform the list of commands, which is the object returned by the getcommandObject () method will execute (). Let's take a look at the parent Class Command class, we exist it in cmd_php4 / command.php file in.

CMD_PHP4 / Command.php

// php 4 class command {function execute () {Die ("Command :: execute () is an abstract method");}}?>

You can see that Command is the implementation of abstract classes in PHP4, we can't directly alive, but must be derived from neutronia and then instantiate. When we use PHP5, we can use better way - using the Abstract keyword to declare the classes and methods as "abstraction":

// PHP 5 Abstract Class Command {Abstract Function Execute ();}?> The following is the implementation of the abstract class above, overridden the execute () method, where you join the actual content. This class is named RealCommand, which can be found in the cmd_php4 / realcommand.php file. cmd_php4 / realcommand.php // PHP 4 require_once 'Command.php'; class realcommand extends Command {function execute () {print "realcommand :: execute () executing as ordered sah / n!";}}?> The use of such The structure can make the code flexible. You can add new Command classes at any time without changing the framework of the periphery. But you have to pay attention to some factors for potential suspension scripts. We need to make sure that the class file exists and exists in this class, and the class is sub-class> (just like Realcommand). In the example, if we try to find the operation of the class fails, the script execution will abort, which reflects the security of the code. But this code is not flexible, there is not enough flexibility. Extreme reflection is that the class method can only perform a positive and positive operation, which is only responsible for finding and instantifying a Command object. It cannot handle errors performed in a larger-wide script (of course, it should not be responsible for handling errors. If we give a class method plus too much association with the surrounding code, then this type of reuse will become difficult, not easy Extension). Although use DIE () avoids the risk of embedded script logic in the getcommandObject () method, it is too intense for the response to the error - immediately abort the program. In fact, we don't want to stop executing programs immediately when you can't find the you want, maybe we have a default command to let the program continue.

转载请注明原文地址:https://www.9cbs.com/read-122700.html

New Post(0)