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

2

2 Answers 2

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

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

7 Comments

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
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).
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
You should 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). 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.
you parse "mysql" in to the class, you could do exactly the same with the array $conn = new DB("mysql",$DB_VALID);
|
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.

1 Comment

highly recomended NOT to use global

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.