0

I am aware of all the great debug engines out there like Zend or xdebug, but I just want a simple error_log("Something"); debug mode.

I have a class like this:

class myClass {

    $DEBUG_MODE = True;

    public function someFunction(){

        if($DEBUG_MODE)
        {
            error_log($varName); // outputs to my Apache server's error log file.
        }
    }
}

However, I get the below error:

Undefined variable: DEBUG_MODE in path in line: integer

I am probably mixing up my Java and PHP... Could someone give me some insight or perhaps a better way to debug?

3 Answers 3

1

You should access class properties somehow (either statically or by object). My answer shows how to do it statically (so every object of myClass can use the same value):

<?php

class myClass {

    // make it static so you don't have to set it in every instance of myClass
    protected static $DEBUG_MODE = True;

    public function someFunction(){

        if(self::$DEBUG_MODE) // reference the static variable from this class
        {
            error_log($varName); // outputs to my Apache server's error log file.
        }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

THanks Kleskowy, that's exactly what I need. Do you think it's better to make the if-statement into a function so I don't need to repeat a bunch of if-statements?
It is an option, and a good way to go I think, because it will make your code more flexible and easier to override (i.e. in class hierarchy). But remember that if you call it 1mln times, it's much better to use 1mln if statements than 1mln function calls.
I defined a function right above my other functions, calling it debugMode($string) (inside that function is error_log($string)). Using it in my functions, I am getting a Call to undefined function debugMode(). Why is it that although I defined the function, I cannot use it?
you should declare it like protected static function debugMode($string) { ... } and then call it like this: if (self::debugMode($str)) { ... }. I think you should read about PHP syntax...
0

To access class properties (and methods), you should use $this->

if ($this->DEBUG_MODE)

Notice that there is no $ behind DEBUG_MODE

Comments

0

try like this :

class myClass {

    private $DEBUG_MODE = True;

    public function someFunction(){

        if($this->DEBUG_MODE)
        {
            error_log($varName); // outputs to my Apache server's error log file.
        }
    }
}

and for logging you can use apache log4php

if you want debug your php code inline ! simply use php vardump function

it's very good tools for debugging and get variable value

var_dump($varName);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.