Php (as the mainstream development language) 5. the following three methods are used to handle program errors before php (as the mainstream development language) 5: 1. use the trigger_error () or die () function to generate a script-level warning or fatal error (fatalerror); Synt
Php (as the current mainstream development language) error handling before 5 There are three methods to handle program errors before php (as the mainstream development language) 5:
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.
// Php (as the mainstream development language) 4
Require_once (pai_php (as the mainstream development language) 4/Command. php (as the mainstream development language ));
Class CommandManager {
Var $ export dir = "pai_php (as the mainstream development language) 4 ";
Function getCommandObject ($ cmd ){
$ Path = "{$ this-> Export Dir}/{$ cmd}. php (as the 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 is not a Command ");
}
Return $ ret;
}
}
?>
This is a simple example of using php (as the mainstream development language) to implement the "Command Pattern design Pattern" (see Java and Pattern). Programmers who use this class (client coder) can put a class into the Directory (in this example, the class is named pai_php (as the mainstream development language) 4 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. we will store it in the plu_php (as the mainstream development language) 4/Command. php (as the mainstream development language) file.
// Php (as the mainstream development language) 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 php (as the mainstream development language) 4. we cannot instantiate it directly, but we must first dispatch the child class in the middle and then instantiate it. When we use php (as the mainstream development language) 5, we can use a better way-use the abstract keyword to declare classes and methods as "abstract ":
// Php (as the mainstream development language) 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 plu_php (as the mainstream development language) 4/realcommand. php (as the mainstream development language) file.
// Php (as the mainstream development language) 4
Require_once Command. php (as the mainstream development language );
Class realcommand extends Command {
Function execute (){
Print "realcommand: execute () executing as ordered sore! ";
}
}
?>
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. We need to ensure that the class file exists and the class exists in the file, and the class is a subclass 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 ).
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