rumd3x/php-go-errors
Composer 安装命令:
composer require rumd3x/php-go-errors
包简介
PHP implementation of golang errors package.
README 文档
README
PHP implementation of golang errors package.
Install
composer require rumd3x/php-go-errors
Example
Here's an example of usage of the standard Error implementation.
For more information consult the API documentation on the section below.
<?php require 'vendor/autoload.php'; use Rumd3x\Errors\Error; $result = exampleFunction(); if ($result instanceof Error) { echo "$result\n"; exit 1; } echo "File contents:\n"; foreach ($data as $row) { echo "$row\n"; } /** * The example function */ function exampleFunction() { $data = file('filename.txt'); if ($data === false) { return Error::new('failed to open file'); } return $data; }
API
This library provides two utilities that can be used and extended upon:
- A standard interface:
Rumd3x\Errors\ErrorInterface; - A standard implementation:
Rumd3x\Errors\Errorthat already implementsRumd3x\Errors\ErrorInterface;
Error (Interface)
An Rumd3x\Errors\ErrorInterface implementation's constructor should accept the error message string as its first and only parameter.
$err = new ErrorImplementation('error message');
It should also provide a public method error to convert the instance into the error message:
$err = new ErrorImplementation('error message'); echo $err->error();
Error (Implementation)
The standard error implementation full classname is Rumd3x\Errors\Error and already implements Rumd3x\Errors\ErrorInterface.
- The standard error implementation contained in this library is not final, so it can be extended upon.
- It can also be safely treated as a string.
It also provides the following extra methods:
new(Static)newf(Static)
new
The new method, allows to create a new instance of error, but statically, it is functionally the same as using the constructor.
$err = Error::new('error message');
newf
The newf method, allows to create a new instance of error, but it can accept an infinite amount of arguments: (It is functionally the same as formatting a string using sprintf then passing it to the new method)
- The first argument should be the error text, but it accepts the
%notation for formatting; - The rest of the parameters should be the arguments to be formatted on the error text;
$err = Error::newf('error: %d is %s than %d', 10, 'less', 20);
Treating error as string
The Rumd3x\Errors\Error or whatever class extend it, can be safely treated as a string, and used alongside one.
$err = Error::new('error message'); $text = $error->error(); // $text should contain 'error message' $text2 = (string) $error; // $text2 should also contain 'error message' echo $err; // should output 'error message' echo "Error: $err"; // should output 'Error: error message'
rumd3x/php-go-errors 适用场景与选型建议
rumd3x/php-go-errors 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 07 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 rumd3x/php-go-errors 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rumd3x/php-go-errors 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-07-11