Table of Contents
回复内容:
Home Backend Development PHP Tutorial php中大量使用die或exit是否有弊端?

php中大量使用die或exit是否有弊端?

Jun 06, 2016 pm 08:44 PM
php

php 中大量使用 dieexit,除了可读性方面是否有其他弊端?

回复内容:

php 中大量使用 dieexit,除了可读性方面是否有其他弊端?

我觉得 die()/exit() 主要是影响程序的设计,也就是影响可读性和未来的可维护性。我自己是坚决抵制无脑使用 die()/exit() 的。

在 php 引入异常机制之前, die()/exit() 是仅有的提前退出程序的方法,用一用还凑合。但有了异常机制之后,应该学习 java 的设计思路:业务的错误用返回值返回,无法处理的请求抛异常。这样的设计保证了调用者无论如何都有足够的控制权,从而让整个系统是健壮的。

在设计工具类和工具函数时,die()/exit() 应该严令禁止,因为它们无权决定整个程序的生死。

在写一个具体业务脚本时,die()/exit() 仅在需要向 shell 返回状态码或者确实确定业务已经结束的情况下使用,其他情况最好禁止使用。

die()exit()都是表示在这里就自然结束,我觉得很正常啊,为什么会有可读性的问题?私以为超长的逻辑嵌套和多重include更有可能造成你所谓的可读性的问题。

exit和die是人为结束脚本运行,顾名思义,可读性肯定没有问题的。
性能方面,直接结束脚本运行,对性能是有益无害。
如果非要说弊端,那我认为是:本来应该用return的时候却使用了exit,可能脚本提前退出,造成功能上的问题。不过,我认为这是写代码的人的问题,和语法结构没有关系。
所以,该用就用吧,放心大胆的用,提供出来就是让人使用的。

对于程序代码书写来说,我同意公子 @公子 dieexit 就是程序结束。

但是对于函数和库的设计来说,我觉得不应该使用这两个,而是使用 return。因为一个函数的调用,不应该将整个程序给停止了(OMG,这个函数的权限也太大了点儿吧)。不管这个函数是正确调用,还是出现问题了,都应该通知他的调用者,方法当然是通过 return 了。

还没有遇到过性能上的问题,但是如果你使用trigger_error()函数来替代die(),你的代码在处理错误上会更具优势,对于客户程序员来说更易于处理错误。
下面是是我转发的一篇文章,你可以看看:http://blog.csdn.net/yipiankongbai/article/details/17568223

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles