0

I've read several of the threads that already exist with this, or a similar, name. None seems to address my situation exactly.

I'm getting this error from my utils.php class.

Notice: Undefined variable: debug in /app/www/utils.php on line 89 Fatal error: Call to a member function debug() on a non-object in /app/www/utils.php on line 89

I define the variable debug like this:

require_once('PHPDebug.php');
$debug = new PHPDebug();

and then call it (in line 89) like this:

$debug->debug($message);

The reason I'm so baffled is that I copied and pasted these lines from my index.php and that call works just fine.

If you want, I can include links to the index.php and utils.php files, as well as PHPDebug.php.

elemjay19
  • 1,126
  • 3
  • 25
  • 51
  • 3
    First of all before line 89 do var_dump($debug); and post what you get. – Aurelio De Rosa Nov 20 '11 at 02:54
  • I put that on the line right before, so new line 89, and got: `Notice: Undefined variable: debug in /app/www/utils.php on line 89 NULL` – elemjay19 Nov 20 '11 at 02:57
  • Is the declaration of $debug before like 89? – Jose Vega Nov 20 '11 at 02:57
  • 2
    Well, you get the problem. For a reason that we can't know because you don't post the whole code, $debug var is no more an instance of PHPDebug but is null. Thus, you can't call a method on a null var. – Aurelio De Rosa Nov 20 '11 at 02:59
  • Could it be because I'm defining $debug outside of any functions in the file (which is not a class) and then I'm accessing $debug (in line 89 etc) within a function? – elemjay19 Nov 20 '11 at 03:06

1 Answers1

3

Thanks to your last comment, the solution to your problem is to declare $debug as global within the function. Thus, you should have something like this:

require_once('PHPDebug.php');
$debug = new PHPDebug();

function myfunction()
{
   global $debug;

   // some code

   $debug->debug($message);
}

You can read more about global in the official doc.

Aurelio De Rosa
  • 21,856
  • 8
  • 48
  • 71