Igor
Igor

Reputation: 171

Variable scope in PHP class

my problem is very straightforward but I can't resolve it.

In my index.php I'am including two PHP files.

    require_once("/lib/config.php");
    require_once("/lib/connect.php");

In config file I declare variable #config

$config = array(  
    "db" => array(  
        "www_db" => array(  
            "username" => "user1",  
            "password" => "pass1",  
            "conn_string" => "blabla"
        )  
    ),  
    "paths" => array("images" => $_SERVER["DOCUMENT_ROOT"] . "/images")  
);  

In connect.php I have a singleton class Connection.

    class Connection
    {

private static $instance = NULL;

public static function getInstance()
{
    if (!self::$instance)
        self::$instance = new Connection();
    return self::$instance;
}


private $conn;

// Create connection to Oracle
public function getConnection() 
{
    //if (INCLUDE_CHECK == true)
//  {
        $conn = oci_connect($this -> $config["db"]["www_db"]["username"], 
                            $this -> $config["db"]["www_db"]["password"], 
                            $this -> $config["db"]["www_db"]["conn_string"]);

My problem is that my Connection class doesn't see $config variable declared in config.php. I have also tried to declare $config as global. I am getting error " Undefined variable: config..." "... in connect.php". Please help.

Upvotes: 0

Views: 399

Answers (4)

casraf
casraf

Reputation: 21684

You have to use the global keyword to specify when you want to include global variables. For instance, this is how it works:

$config = array ('one' => 'two');

class Foo {
    public function bar() {
        print_r($config); // NULL
        global $config;
        print_r($config); // Array ( 'one' => 'ywo' )
    }
}

Upvotes: 2

Igor
Igor

Reputation: 171

I solve it like this:

private $username = "usr1";
private $password = "pass1";
private $conn_string = "connection_string";

private $conn;

// Create connection to Oracle
public function getConnection() 
{
    $conn = oci_connect($this -> username, 
                        $this -> password, 
                        $this -> conn_string);
    if (!$conn) {
       $m = oci_error();
       echo $m['message'], "\n";
       exit;
    }
    else
    {
        return $conn;
    }
}

Upvotes: 0

Shaun Hare
Shaun Hare

Reputation: 3871

You can't see the variable inside a class as it is declared outside. Pass it to the class, use Dependency Injection (either setter or constructor injection) and then $config will be available in your connection class.

Upvotes: 3

Mr. B.
Mr. B.

Reputation: 8697

I recommend adding a new setter-method to your Connection.class, like:

public function set_config($config = array()) {
    if (empty($config)) return false;

    $this->_username = $config["username"];
    $this->_password = $config["password"];
    // ... 
}

Then you're able to use the data in other methods like:

oci_connection($this->_username, $this->_password);

Otherwise you set the $config global:

global $config;

Upvotes: 0

Related Questions