imliam/php-catch-exit
Composer 安装命令:
composer require imliam/php-catch-exit
包简介
Gracefully handle an unwanted exit statement.
关键字:
README 文档
README
Gracefully handle an unwanted exit statement.
🤔 F.A.Q.
Why would you need this?
This shouldn't be needed at all, really. There is virtually no practical reason to have an exit statement over an exception in a modern PHP application, as all frameworks have their own decent, extendable exception handlers that should be taken full advantage of.
That said, a lot of beginners PHP tutorials end up showing the same sort of code for your first database connection:
<?php $db = mysql_connect(...) or die('Could not connect.');
Luckily this is a thing of the past for the most part, but unfortunately still seems to be a practice some people use. Over the past few years, I still occasionally come across Composer packages that still use exit and die statements when an error occurs.
These packages should be avoided entirely - or better yet, fork or submit a pull-request when they pop up.
So why do this?
This package/function is more of a proof-of-concept to show how these situations can be handled, rather than a serious suggestion of a way to keep working with them.
💾 Installation
You can install the package with Composer using the following command:
composer require imliam/php-catch-exit:^1.0.0
📝 Usage
If you've ever written a basic exit statement before, you'll understand that, if the output buffer is empty at that point, you'll be greeted with a blank white screen in the browser with text given to the statement.
exit('Something went wrong.');
This package adds a single helper function that will accept a closure as the first argument. This closure is where you can add any code that you expect could throw an exit statement - just like the try block of a try-catch statement.
Assuming no exit occurs, you can return a value from this closure and continue your application like normal.
$var = catch_exit(function() { return "It's all okay."; }, ...); echo $var; // "It's all okay."
Print a string
The second argument of the function can accept a string which will be output to the buffer if an early exit occurs in the closure from the first argument.
This lets you decide what gets shown instead of the previous content, which may be out of your control.
catch_exit(function() { exit('Uh-oh!'); }, 'Something went wrong.'); // 'Something went wrong.' is displayed
You can also use this to render a view that can be displayed to the user in case this occurs.
Execute a closure
However, the second argument can also be another closure which will only be executed if there is an early exit occurring in the first closure. It'll also give you access to the current output buffer
Here, you can gracefully handle the way the application shuts down. You may want to perform some actions such as:
- Log the error that occurred
- Flash a message to the session to inform the user what happened
- Render an error page to the user
- Redirect the user back to the previous page
catch_exit(function() { exit('Uh-oh, something went wrong!'); }, function($message) { Log::error("The application exited with the following message: '{$message}'"); return View::make('errors.500') ->with('error', 'An unexpected error occurred.'); });
This can also be used to handle fatal errors - such as when a class that doesn't exist is being instantiated or when the request exceeds the maximum execution time for PHP.
catch_exit(function() { new ClassThatDoesNotExist(); }, function($message) { // ... });
🔖 Changelog
Please see the changelog file for more information on what has changed recently.
⬆️ Upgrading
Please see the upgrading file for details on upgrading from previous versions.
🎉 Contributing
Please see the contributing file and code of conduct for details on contributing to the project.
🔒 Security
If you discover any security related issues, please email liam@liamhammett.com instead of using the issue tracker.
👷 Credits
♻️ License
The MIT License (MIT). Please see the license file for more information.
imliam/php-catch-exit 适用场景与选型建议
imliam/php-catch-exit 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 335 次下载、GitHub Stars 达 4, 最近一次更新时间为 2018 年 09 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「imliam」 「php-catch-exit」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 imliam/php-catch-exit 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 imliam/php-catch-exit 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 imliam/php-catch-exit 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
An easier way to define custom Blade directives.
Utility class to validate, format and generate NHS numbers.
Utility class to validate, format and generate NHS numbers.
Ensure that a Gmail address is unique
Set a .env file variable from the command line
Present names for English-language applications.
统计信息
- 总下载量: 335
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-09-08