The variable $DB_VALID
does not exist inside the __construction
function scope.
The recommended solution is that you move $DB_VALID
to a static variable inside the DB
class, as such:
class DB {
static $DB_VALID = array("mysql");
function __construct($conn) {
if(in_array($conn,self::$DB_VALID)) {
echo "exists!";
}
else {
echo "doesnt exist";
}
}
}
You can later access that array in other parts of your code by referencing it as DB::$DB_VALID
.
However, if you must keep the global variable and access it from within __construct
, you can use the global
keyword to bring it into the local scope, as such:
$DB_VALID = array("mysql");
class DB {
function __construct($conn) {
global $DB_VALID; // Brings the global variable to local scope
if(in_array($conn,$DB_VALID)) {
echo "exists!";
}
else {
echo "doesnt exist";
}
}
}
Please consider the first solution on the future, though, as using global variables is an easy way to have your applications break as they evolve.
Edit: As you mentioned in the comments your restriction is the order you're loading your scripts right now, you should also really look into class autoloading and namespaces, as your projects will get increasingly complex and harder to manage otherwise (see sitepoint.com/autoloading-and-the-psr-0-standard).