Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4094668
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:46:26+00:00 2026-05-20T19:46:26+00:00

I am working on some PHP classes that can do some basic profiling, debugging,

  • 0

I am working on some PHP classes that can do some basic profiling, debugging, and error handling. Yes I know about XDebug and it’s great, but I would like to have some classes as well for my library to do some of this type of stuff.

Below is a class I am working on that will show an error message when one occurs and will show me the line number it happened on as well as the source code for that line and the X amount of lines below and above the error line. Similar to in this image below… (may have to view image in new window/tab to see larger version)

source code class

Here is my class code I have as of right now for this…

<?php

class Debugger
{
    public static function debug_source($file, $line_number, $padding = 5)
    {
        if (!$file or !is_readable($file))      {
            return false;
        }

        // Open the file and set the line position
        $file = fopen($file, 'r');
        $line = 0;

        // Set the reading range
        $range = array('start' => $line_number - $padding, 'end' => $line_number + $padding);

        // Set the zero-padding amount for line numbers
        $format = '% ' . strlen($range['end']) . 'd';

        $source = '';
        while (($row = fgets($file)) !== false)
        {
            // Increment the line number
            if (++$line > $range['end'])
                break;

            if ($line >= $range['start']){
                // Make the row safe for output
                $row = htmlspecialchars($row, ENT_NOQUOTES);
                // Trim whitespace and sanitize the row
                $row = '<span class="number">' . sprintf($format, $line) . '</span> ' . $row;
                if ($line === $line_number){
                    // Apply highlighting to this row
                    $row = '<span class="line highlight">' . $row . '</span>';
                } else{
                    $row = '<span class="line">' . $row . '</span>';
                }
                // Add to the captured source
                $source .= $row;
            }
        }
        // Close the file
        fclose($file);

        echo '<div id="exception_error">';
        echo '  <h1><span class="type"> Error [ 345 ]</span></h1>';
        echo '  <div class="content">';
        echo '      <p>Test error message on line number ' . $line_number. '</p>';
        echo '      <p><pre class="source"><code>' . $source . '</code></pre></p>';
        echo '  </div>';
        echo '</div>';
    }
}

// Testing the class above.      
$file = 'bitmask/bitmasktest.php';
$line_number = 82; 
$line_number_padding = 5;

$debugger = new Debugger();
$debugger->debug_source($file, $line_number, $line_number_padding);

?> 

So I am looking for ways to improve and add on to this. It is not complete yet, this is just a start.

A couple of basic questions.

1) Should I declare all variables in a class like this at the top? For example in this class I access the properties like this $file, $line_number, $padding. Should I declare them at the top (all properties) like…

<?php
//public/private/protected 
public $this->file;
private $this->$line_number;
private $this->padding;
// etc, etc, for every single property used in a class method
// or should I access them how I currently do in 
// this class like $file, $line_number, etcc????

?>

?>

2) To make the output look correct from my method above, it needs this CSS data below in the page, I know I should probably include a CSS file for it or add it to my other css files, but my goal is to have this class be self contained, so the class can be ran anywhere and have the correct output. Should I somehow have a method that outputs the CSS needed for this class or what are some good ideas to keep it self contained and still look the way I want it (like in the image)?

<style type="text/css">
#exception_error {
    background: #ddd;
    font-size: 1em;
    font-family:sans-serif;
    text-align: left;
    color: #333333;
}
#exception_error h1,
#exception_error h2 {
    margin: 0;
    padding: 1em;
    font-size: 1em;
    font-weight: normal;
    background: #911911;
    color: #FFFFFF;
}
#exception_error h1 a,
#exception_error h2 a {
    color: #FFFFFF;
}
#exception_error h2 {
    background: #666666;
}
#exception_error h3 {
    margin: 0;
    padding: 0.4em 0 0;
    font-size: 1em;
    font-weight: normal;
}
#exception_error p {
    margin: 0;
    padding: .4em;
}
#exception_error a {
    color: #1b323b;
}


#exception_error p {
    margin: 0;
    padding: 0.1em 0;
}

#exception_error pre.source {
    margin: 0 0 1em;
    padding: 0.4em;
    background: #fff;
    border: dotted 1px #b7c680;
    line-height: 1.2em;
}

#exception_error pre.source span.line {
    display: block;
}

#exception_error pre.source span.highlight {
    background: #f0eb96;
}

#exception_error pre.source span.line span.number {
    color: #666;
}


</style>




<style type="text/css"> 
.linenum{ 
    text-align:right; 
    background:#FDECE1; 
    border:1px solid #cc6666; 
    padding:0px 1px 0px 1px; 
    font-family:Courier New, Courier; 
    float:left; 
    width:17px; 
    margin:3px 0px 30px 0px; 
    } 

code    {/* safari/konq hack */ 
    font-family:Courier New, Courier; 
} 

.linetext{ 
    width:700px; 
    text-align:left; 
    background:white; 
    border:1px solid #cc6666; 
    border-left:0px; 
    padding:0px 1px 0px 8px; 
    font-family:Courier New, Courier; 
    float:left; 
    margin:3px 0px 30px 0px; 
    } 

br.clear    { 
    clear:both; 
} 

</style> 

Thank you for any help, I realize these are some really newby like questions but I do need some help, I don’t want to pick up any bad habits.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T19:46:27+00:00Added an answer on May 20, 2026 at 7:46 pm

    First: the way to declare class properties is like this:

    class Debugger {
        public $file;
        private $line_number;
        private $padding;
    }
    

    Now, class properties only make sense if they remain constant between operations (function calls) performed on the same object — in other words, if their values can be “reused” between calls. In your case, the only one of these which it makes sense to reuse is $padding (set it once and then call debug_source multiple times with the same padding). The other two should remain function parameters.

    Second:

    There is absolutely no way you can make Debugger be self-container and have it produce valid HTML in the context of your page at the same time, unless you make all those styles inline. Of course this solution is far from optimal, but it’s the only one that you can use in this case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some issues with a PHP file that is not working properly. The
I'm working on an app that has PHP classes to connect to the web
I am working on some PHP classes, I have a session class used to
I'm working with some old PHP code that has a lot of the following:
I am working on some form coding in PHP, and I have everything working
I'm a PHP newbie working a some scripts to display some news articles from
I have been working on some code to submit an uploaded image to PHP
Im working on a shopping cart script in php and need some advice on
I am working on a PHP-Mysql based website which has some large lists to
I have my php app working fine, performing some other Graph API functions. However,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.