1

Very simple question here. Doing my first OO programming in PHP, more used to Java & C#.

With this class definition:

class RouteParser {
    var $DEBUG = 1;

    function parse($from, $to, $route){
        debug("test");
        debug("test again");
    }

    function debug($arg) {
        if($DEBUG) {
            echo "<pre>DEBUG: " . print_r($arg) . "</pre>";
        }
    }
}

And this executing code:

include("RouteParser.php");
$rp = new RouteParser();
$return = $rp->parse("from","to","test");

I'm getting the error:

Fatal error: Call to undefined function debug() in C:\xampplite\htdocs\routeparse\RouteParser.php on line 7

PHP doesn't support function prototypes as far as I can tell, so I'm stumped on how to tell one class function that another class function exists... according to what I found on the PHP documentation, this is only an issue if the function is inside a conditional (which makes sense)...

1 Answer 1

11

Use $this->debug() instead. $this makes PHP look for the function in the class itself, while just debug() makes PHP look for a function called debug in the global scope.

Sign up to request clarification or add additional context in comments.

2 Comments

Ahh, that makes sense. Well, it doesn't, compared to other languages, but I understand what you are saying... ;-)
If the method is static, you would refer to it inside the class itself with self::debug().

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.