具有新特性的PHP5之异常处理机制
php(做为现在的主流开发语言)5之前的错误处理
在php(做为现在的主流开发语言)5之前的程序错误处理多使用以下三种办法:
1.使用trigger_error()或die()函数来生成一个脚本层次的警告(warning)或致命错误(fatal error);
2.在类方法或函数中返回一个错误标记(如false),也可能设置一个之后可以检查的属性或全局变量(如$error),然后在适合的地方检验其值再决定是否继续执行程序(如if($error==1){});
3.使用PEAR处理错误;
(一)使用die()或trigger_error()
你可以使用die()函数来结束程序运行。以下是一个简单的类,它尝试从一个目录中加载一个类文件。
代码列表 index.php(做为现在的主流开发语言) ?>
(做为现在的主流开发语言)
// php(做为现在的主流开发语言) 4
require_once(cmd_php(做为现在的主流开发语言)4/Command.php(做为现在的主流开发语言));
class CommandManager {
var $cmdDir = "cmd_php(做为现在的主流开发语言)4";
function getCommandObject($cmd) {
$path = "{$this->cmdDir}/{$cmd}.php(做为现在的主流开发语言)";
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;
}
}
?>
这是一个用php(做为现在的主流开发语言)实现“Command Pattern设计模式”的简单例子(请参看《Java与模式》)。使用这个类的程序员(客户程序员client coder)可以将一个类放到目录中(例中为cmd_php(做为现在的主流开发语言)4目录)。一旦文件和其中包含的类同名,并且这个类是Command类的子类,我们的类方法将生成一个可用的Command对象。Command类中定义了一个 execute()方法用来执行找到的命令,即 getCommandObject()方法返回的对象将执行execute().
我们再看看父类Command类,我们将它存在cmd_php(做为现在的主流开发语言)4/Command.php(做为现在的主流开发语言)文件中。
代码列表cmd_php(做为现在的主流开发语言)4/Command.php(做为现在的主流开发语言) ?>
(做为现在的主流开发语言)
// php(做为现在的主流开发语言) 4
class Command {
function execute() {
die("Command::execute() is an abstract method");
}
}
?>
你可以看到,Command是php(做为现在的主流开发语言)4中抽象类的实现,我们无法直接将其实例化,而必须先从中派生中子类然后再实例化。当我们使用php(做为现在的主流开发语言)5后,我们可以使用更好的方式—使用abstract关键字将类和方法声明为“抽象”:
代码列表 ?>
(做为现在的主流开发语言)
// php(做为现在的主流开发语言) 5
abstract class Command {
abstract function execute();
}
?>
下面是对上面的抽象类的实现,覆写了execute()方法,在其中加入真正可以执行的内容。这个类命名为realcommand,可以在cmd_php(做为现在的主流开发语言)4/realcommand.php(做为现在的主流开发语言)文件中找到。
代码列表 cmd_php(做为现在的主流开发语言)4/realcommand.php(做为现在的主流开发语言) ?>
(做为现在的主流开发语言)
// php(做为现在的主流开发语言) 4
require_once Command.php(做为现在的主流开发语言);
class realcommand extends Command {
function execute() {
print "realcommand::execute() executing as ordered sah!
";
}
}
?>
使用这样的结构可以使代码变得很灵活。你可以在任何时候增加新的Command类,而不需要改变外围的框架。但是你不得不注意一些潜在的中止脚本执行的因素。我们需要确保类文件存在,并且在文件中该类存在,并且该类是Command的子类(就像realcommand一样)。
在例子中,如果我们尝试寻找类的操作失败,脚本执行将会中止,这体现了代码的安全性。但这段代码不灵活,没有足够的弹性。极端的反映是类方法只能进行积极正面的操作,它只负责找出和实例化一个Command对象。它无法处理更大范围内脚本执行的错误(当然它也不应该负责处理错误,如果我们给某个类方法加上太多与周边代码的关联,那么这个类的重用将会变得困难,不易扩展)。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
