0

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.

1
  • 1
    If you include a file with variables, you can't use $this because it refers to properties of the current object. Commented May 12, 2013 at 15:48

4 Answers 4

3

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.

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

1 Comment

Hi Shaun, yes, that would work except the methods that need the DB info are all static (essentially alternate constructors) I could pass in the DB info as an arg to these, but in the interest of simplicity, I used the global keyword to pull them in from global scope. (See Chen's answer below). Thanks though!
2

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' )
    }
}

1 Comment

Thanks Chen! This is how I solved it because I'm using these inside static methods so the constructor injection and setter ways are not useful to me.
0

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;

4 Comments

@Havelock You're right, but I guess Exception-Handling is another chapter - I try to keep the example-code as simple as possible. :-)
Like I wrote, I use statement global $config but still doesn't see $config variable.
did you try the setter-method?
@Igor Please post the solution, if it's possible. It could help anyone else who has the same problem. Thank you!
0

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;
    }
}

Comments

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.