我将从一个最小的例子开始。假设我们的项目中有以下依赖项:
<?php
namespace VeryUsefulFilesystemLibrary;
function read(string $filepath): string|false
{
return file_get_contents($filepath);
}
我们的项目看起来像:
<?php
require 'vendor/autoload.php';
$filedata = VeryUsefulFilesystemLibrary\read('/path/to/file');
// do something depending on $filedata (signature: string|false)
在某个时候(可能是在开发的早期),我们决定使用自定义错误处理程序,例如filp/whoops
:
<?php
require 'vendor/autoload.php';
(new Whoops\Run)
->pushHandler(new Whoops\Handler\PlainTextHandler)
->register();
$filedata = VeryUsefulFilesystemLibrary\read('/path/to/file');
// how about Whoops\Exception\ErrorException now? :)
现在所有以前记录但正确处理的潜在警告,现在正在终止应用程序。我可以使用try/catch
,但首先,例如fopen()
看起来try/catch
非常奇怪,其次,我必须考虑更多。多得多。而且……我什至找不到词,这怎么……错了?
就个人而言,我更希望 PHP 中的任何问题都是例外。Whoops 对他的做法一模一样。但是我不明白如何使用默认行为不同的语言以这种方式工作,因此没有必要的文档(同样适用于依赖项)。
我错过了什么吗?