why is this __autoload giving me e_notice warning when using a defined constant?

152 Views Asked by At

I'm getting for some reason this notice in the code bellow, my application is working fine and it's not affecting the rest of code in any way. But I can't wrap my head around this one notice. I don't see any errors in my code. Also, I'm using _ROOT global constant in other places and it's not giving me any notice about it being undefined. Interestingly the if (defined('_ROOT')) evaluates to true as it's supposed to be, since obviously it's really for sure defined.

Code:

<?php

session_start();

//define('_DEBUG', 'YES');
define('_ROOT', dirname(__FILE__), true);

require_once _ROOT.'/config/config.php'; //no notice

function __autoload($class_name) {
   if (defined('_DEBUG')) { echo '__autoload called<br>'; }
   if (defined('_ROOT')) { echo 'root exists'._ROOT.'<br>'; } //doesn't give me a notice
   if (file_exists(_ROOT.'/app/core/'.$class_name.'.php')) { //gives me a notice
      require_once _ROOT.'/app/core/'.$class_name.'.php'; //doesn't give me a notice
   }
}

$app = new Application();
echo $app->run();

Notice: Notice: Use of undefined constant _ROOT - assumed '_ROOT' in path\to\index.php on line 13

1

There are 1 best solutions below

1
Rijk On

It's obvious: _ROOT is not defined at the moment __autoload() is called (note that this can be very early in the execution of your script).

if (defined('_ROOT')) { echo 'root exists'._ROOT.'<br>'; }

doesn't give you a notice because the code within the if is never executed when _ROOT is not defined.