Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中处理应用程序级错误_Php_Oop_Class_Error Handling - Fatal编程技术网

在PHP中处理应用程序级错误

在PHP中处理应用程序级错误,php,oop,class,error-handling,Php,Oop,Class,Error Handling,我想知道是否有人能向我展示一种处理PHP应用程序中错误的好方法,我也可以轻松地在其他脚本中重用这种方法 到目前为止,我一直在使用以下功能: 内联错误 function display_errors_for($fieldname) { global $errors; if (isset($errors[$fieldname])) { return '<label for="' .$fieldname. '" class="error">' .

我想知道是否有人能向我展示一种处理PHP应用程序中错误的好方法,我也可以轻松地在其他脚本中重用这种方法

到目前为止,我一直在使用以下功能:

内联错误

function display_errors_for($fieldname) {
    global $errors;

    if (isset($errors[$fieldname]))
    {
        return '<label for="' .$fieldname. '" class="error">' . ucfirst($errors[$fieldname]). '</label>';
    } else {
        return false;
    }
}

请分享您的想法,如果这是一种制作可重用类来存储和显示整个应用程序的错误的好方法,或者是越来越熟悉异常并尝试/捕获我的唯一选择?

如果您喜欢OOP,您应该使用类似Zend framework的框架。在ZF中,有一个用于一般错误/结果消息的操作帮助程序:FlashMessenger


我无法想象为什么要创建一个独特的类来完成像在浏览器中回显多个文本字符串这样琐碎的任务。
或者我错过了什么

我甚至看不出这个函数有什么意义

就我个人而言,我的模板(我需要的地方)中有这样的代码块

<? if ($data['errors']): ?>
<div class="errors">
  <ul>
<?   foreach ($data['errors'] as $e): ?>
    <li><?=$e?></li>
<?   endforeach ?>
  </ul>
</div>
<? endif ?>


我发现flashMessenger并不是一种处理错误的方法,它只是一个操作助手,允许您在两个操作之间保存消息,并使用$_会话进行请求。实际上@Zubair1要求的并不是真正的错误处理,而是类似于验证
$errors['username']=“用户名不能少于3个字符。”根据你发送的链接,这不是我要找的。它只是显示了使用会话将消息从一个页面传输到另一个页面的方法
FlashMessenger帮助程序允许您传递用户在下一次请求时可能需要查看的消息。
异常并不是很复杂,您应该熟悉它们-最终,它们很可能会简化您的代码。
if (strlen($username) < 3) {
    $errors['username'] = "usernames cannot be less then 3 characters.";
}
class Errors
{
    public $errors = array();

    public function __construct()
    {
        // Initialize Default Values

        // Initialize Methods
    }

    public function __destruct()
    {
        //nothing here too...
    }

    public function add($errorField, $errorDesc)
    {
        if (!is_string($errorField)) return false;
        if (!is_string($errorDesc)) return false;
        $this->errors[$errorField] = $errorDesc;
    }
    public function display($errorsArray)
    {
        // code to iterate through the array and display all errors.
    }
}
<? if ($data['errors']): ?>
<div class="errors">
  <ul>
<?   foreach ($data['errors'] as $e): ?>
    <li><?=$e?></li>
<?   endforeach ?>
  </ul>
</div>
<? endif ?>