W
W
wartur2011-11-07 05:00:54
PHP
wartur, 2011-11-07 05:00:54

PHP - Display errors in the right place in the template?

Hello PHP-oriented habralyudi.
There was a serious question on which on did not find the decision.
Conditions:
- There is not so hot what template, there is not so hot what PHP code. The code boils, boils, and prepares the result. Next, the template engine is turned on and outputs this result. Everything would be fine if there were no warnings in the process of boiling the code. If they appear, then they are displayed before the template, and the template has all sorts of styles and other things that these warnings turn into an inconspicuous pixel on top of the browser (probably met such a situation).
- If there is a fatal error, the template itself will not be displayed and the error will be clearly visible, there are no problems with this. There are problems with warnings and comments.
Task:
accumulate these warnings into a variable and output them to the desired and easily readable place in the template.
Dripped literature like this, stderr gave no results:
php.net/manual/en/wrappers.php.php
janicky.com/content/article_18.html
php run in mod_php mode
=======
I don't see a way how via logs, but it's a crutch!
Let's gather a consortium, has already given its contribution. Do you have any ideas?
=======
UPD:
Thanks to those who answered the question and signed this question to favorites, apparently it is the best one in the last 2 weeks, I add my answer to this question:
Sometimes there is a problem with the fact that it is not bad to intercept errors with OOP for this, the set_error_handler function is set to METHOD like this:

// Classes<br/>
class BaseCore implements IBaseCore<br/>
{<br/>
 const ERRROR_LAYOUT = true;<br/>
<br/>
public function __construct()<br/>
 {<br/>
 // Установка перехвадчика ошибок<br/>
 set_error_handler(array(&$this, 'error_handler'));<br/>
 }<br/>
 <br/>
 public function error_handler($errno, $errstr, $errfile, $errline, $errcontext)<br/>
 {<br/>
 return false; // функция отключена, использовать базовые установки<br/>
 }<br/>
 <br/>
 public function __call($method, $arg)<br/>
 {<br/>
 throw new ErrorFatalBaseException(self::ERRROR_LAYOUT, $method);<br/>
 }<br/>
}<br/>

Hope it helps, good luck

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vitaly Zheltyakov, 2011-11-07
@wartur

For PHP bug tracking: For MySQL bug tracking: I think the principle is clear.
// Устанавливаем обработчик ошибок (в рабочей версии закомментить)
set_error_handler('writeerror');
function writeerror($errno, $errstr, $errfile, $errline) {
// Вы водим ошибку. writelog - пользовательская функция. Используйте свою
writelog('php_error', date("y.m.d H:m:s")."\t".$errno."\t".$errstr."\t".$errfile."\t".$errline);
// Возвращаем true чтобы продолжить работу
return true;
}

function sql_query($query) {
// Подключаем базу
dbconnect();
$return = mysql_query($query);
$error = mysql_error();
if ($error=='') {
return $return;
}
else {
writelog('sql_error', date("y.m.d H:m:s")."\t".$error);
return false;
};
}

V
vvnick, 2011-11-07
@vvnick

It's also a good idea to check PHP settings (in php.ini) such as display_errors and error_reporting.
And make sure that in production errors will simply not be displayed, but only processed by your errorHandlerom.

P
powder96, 2011-11-07
@powder96

Everything is very simple. Initialize the variable in which you will add the errors. Then, put some function as an error handler. After that, you make the function add the error to a variable. Ready.

$errors = '';

set_error_handler('errorHandler'); // обязательно почитайте про это функцию на php.net

function errorHandler($errno, $errstr, $errfile, $errline) {
    global $errors;
    $errors .= 'An error occured. ' . $errstr . ' at ' . $errfile . ':' . $errline . '<br />'
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question