开发者

PHP error line numbers completely off

I don't know what's going on with my PHP. I'm using a bunch of classes and a semi-MVC framework that I developed myself (as an experiment). So the PHP file is including a lot of class files.

My PHP line numbers for error messages are completely off and therefore useless and making it impossible for me to debug. For example, right now I'm getting an error message that says:

`Parse error: syntax error, unexpected ')' in /view.php on line 209

The only problem is: there is no ')' anywhere near line 209. Even worse, if I put die() on line 200 or so, it still gives me an error message, now pushed down to line 210. So clearly the line of code is being taken into account, yet for some reason it's not dying.

Another thing is, it's including a header.php file prior to this line. The header file basically just outputs some HTML, and works fine on other pages. Yet on this page, it doesn't even output the header; it's just dying with a blank page and that error message.

Is 开发者_如何学Pythonthere anything I can do to use more reliable debugging? If I could have an accurate line number, I'm sure I'd find the bug easily.

Edit: I found the bug. The point of this question is not to solve the unexpected ')' bug. The point is: why are the line numbers inaccurate? The actual error message was on line 218, not 209 or 210.


It's your line endings. Open up a program where you can change the line endings and change it to that of the server and the line number will be correct. I don't know about windows, but on mac you can change line endings using TextWrangler (or BBEdit) and on linux using gedit.


Try adding a Logging function with a simple stacktrace.

Use it:

<?php
require_once("Log.class.php");
$myLog = new Log();

$log->write("Tag here", "Details here");
?>

Log.class.php:

<?php

    $debugFolder = "/";

    class ExpectedParamaterUndefinedException extends Exception {}

    class Log {

        private $logs = array();    

        function __contruct(){    
        }

        function write($type, $message){

            $trace=debug_backtrace();
            $caller=array_shift($trace);
            $fileParts = explode("/", $caller['file']);
            $filePieces =  explode(".", $fileParts[count($fileParts) -1]);
            $this->logs[  $filePieces [count($filePieces ) - 2] ] .= " [ ".date(DATE_RFC822)." ] [ $type ] [Line : ".$caller['line']."] ::: $message \n ";
        }

        function __destruct(){
            foreach($this->logs as $caller => $log){
                $fp=fopen($debugFolder . "debug/" . $caller.".log", "a");
                    fwrite($fp, $log);
            fclose($fp);
            }
        }
    }
?>

You will end up with a .log file for each class that you log.

Happy hunting!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜