1

Alright so I am trying to simply check if there is something inside the array from construct but it doesnt seem to work at all...

    $DB_VALID = array("mysql");

    class DB {
        function __construct($conn) {
            if(in_array($conn,$DB_VALID)) {
                echo "exists!";
            }
            else {
                echo "doesnt exist";
            }
        }
    }

Now, since construct is inside a class, if I dump it I will get results saying NULL, but if I dump it outside the construct I will simply get the real results...

Usage

$conn = new DB("mysql");

Results? in_array returns false

DfKimera
  • 2,076
  • 2
  • 21
  • 39
  • $DB_VALID is outside the class scope –  Jan 04 '17 at 03:01
  • 1
    Possible duplicate of [Use a variable from \_\_construct() in other methods](http://stackoverflow.com/questions/15811232/use-a-variable-from-construct-in-other-methods) – tyteen4a03 Jan 04 '17 at 03:11

2 Answers2

2

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).

DfKimera
  • 2,076
  • 2
  • 21
  • 39
  • But is there any other way I can have it outside the scope itself? Way to make it global or such. I dont want of it to be inside the class itself thats the main problem – Tomislav Nikolic Jan 04 '17 at 03:03
  • Sorry, just edited to clear that up. You can reference it by using `DB::$DB_VALID` elsewhere in the code (as long as the class is included beforehand or autoloaded). – DfKimera Jan 04 '17 at 03:04
  • The problem is, I am using a separate php file to include that array (with many other things of course) so therefore using it inside the class itself is somewhat impossible at the moment, I really want to keep it outside of it – Tomislav Nikolic Jan 04 '17 at 03:05
  • You should really look into class autoloading and namespaces, as your projects will get increasingly complex and harder to manage otherwise (see https://www.sitepoint.com/autoloading-and-the-psr-0-standard/). However, you can quickly fix your case by adding `global $DB_VALID` inside `__construct` so the global variable is imported to the function scope. I should warn you, this is **really** not recommended, and goes against best practices. – DfKimera Jan 04 '17 at 03:07
  • you parse "mysql" in to the class, you could do exactly the same with the array `$conn = new DB("mysql",$DB_VALID);` –  Jan 04 '17 at 03:07
  • -1 - does not answer the question of how to use global variables in a class context (actually a duplicate of [this](http://stackoverflow.com/questions/15811232/use-a-variable-from-construct-in-other-methods)). – tyteen4a03 Jan 04 '17 at 03:11
  • @tyteen4a03 I've edited the original answer to provide both the *best practice* option and the *direct solution* option I had suggested in the comments. – DfKimera Jan 04 '17 at 03:17
0

If you have to, use the global keyword.

$DB_VALID = array("mysql");

class DB {
    function __construct($conn) {
        global $DB_VALID; // Makes the variable available in the scope
        if(in_array($conn,$DB_VALID)) {
            echo "exists!";
        }
        else {
            echo "doesnt exist";
        }
    }
}

$conn = new DB("mysql"); // will print "exists!"

Note that global variables usually hints organisation problems, so you should probably review your structure and see if it's really necessary to use global here.

tyteen4a03
  • 1,812
  • 24
  • 45