Exception handling mechanism of PHP5 with new features _php tutorial

Source: Internet
Author: User
   PHP (as current mainstream development language) error handling before 5
The following three methods are used in the process of error handling in PHP (the current mainstream development language) before 5:
1. Use the Trigger_error () or Die () function to generate a script-level warning (warning) or fatal error (fatal error);
  
2. Returns an error token (such as false) in a class method or function, or a property or global variable (such as $error) that can then be checked, and then, where appropriate, to test its value before deciding whether to continue executing the program (such as if ($error ==1) {});
  
3. Handling errors using Pear;
  
(i) using die () or Trigger_error ()
You can use the Die () function to end a program run. The following is a simple class that attempts to load a class file from a directory.
  
  
   PHP (as the current mainstream development language) 4
Require_once (cmd_php (as the current mainstream development language) 4/command.php (as the current mainstream development language));
Class Commandmanager {
var $cmdDir = "cmd_php (as the current mainstream development language) 4";
  
function Getcommandobject ($cmd) {
$path = "{$this->cmddir}/{$cmd}.php (as the current mainstream development language)";
if (!file_exists ($path)) {
Die ("Cannot find $path");
}
Require_once $path;
  
if (!class_exists ($cmd)) {
Die ("Class $cmd does not exist");
}
  
$ret = new $cmd ();
if (!is_a ($ret, Command)) {
Die ("$cmd are not a Command");
}
return $ret;
}
}
?>
  
This is a simple example of implementing the "Command pattern design pattern" in PHP (which is the mainstream development language of today) (see Java and Patterns). Programmers using this class (Client programmer client Coder) can put a class in the directory (for example, cmd_php (as the current mainstream development language) 4 directory). Once the file and the same name are 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 to execute the found commands, i.e. the object returned by the Getcommandobject () method executes execute ().
  
Let's look at the parent Command class and we'll have it in the cmd_php (as the current mainstream development language) 4/command.php (as the current mainstream development language) file.
  
  
  
   PHP (as the current mainstream development language) 4
Class Command {
function Execute () {
Die ("Command::execute () was an abstract method");
}
}
?>
  
As you can see, command is the implementation of the abstract class in PHP (now the mainstream development language) 4, and we cannot instantiate it directly, but we must first derive the neutron class from it and then instantiate it. When we use PHP (as the current mainstream development language) 5, we can use a better approach-using the abstract keyword to declare classes and methods as "abstract":
  
  
  
   PHP (as the current mainstream development language) 5
Abstract class Command {
Abstract function execute ();
}
?>
  
  
The following is an implementation of the above abstract class, which covers the Execute () method, in which content that is actually executable can be added. This class is named Realcommand and can be found in cmd_php (as the current mainstream development language) 4/realcommand.php (as the current mainstream development language) file.
  
  
  
   PHP (as the current mainstream development language) 4
Require_once command.php (as the current mainstream development language);
Class Realcommand extends Command {
function Execute () {
Print "Realcommand::execute () executing as ordered sah!";
}
}
?>
  
Using such a structure can make your code flexible. You can add a new command class at any time without changing the perimeter frame. But you have to pay attention to some potential factors that abort the execution of the script. We need to make sure that the class file exists and that the class exists in the file, and that the class is a subclass of command (just like Realcommand).
  
In the example, if we try to find the operation of the class fails, the execution of the script will abort, which demonstrates the security of the code. But this code is not flexible, not enough flexibility. The extreme reflection is that the class method can only perform positive actions, and it is only responsible for locating and instantiating a command object. It cannot handle the larger range of script execution errors (and of course it should not be responsible for handling errors, and if we add too much association to the perimeter code to a class method, the reuse of this class will become difficult and not easy to expand).

http://www.bkjia.com/PHPjc/509137.html www.bkjia.com true http://www.bkjia.com/PHPjc/509137.html techarticle PHP (as the current mainstream development language) 5 error handling before PHP (as the current mainstream development language) 5 before the program error handling more use the following three ways: 1. Use Trigg ...

  • Related Article

    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.