1

My aim is to retrieve some data from a global array which is defined in another PHP file. My code is running inside database.php file and the array I want to use is inside config.php file. I understand that accessing a global array inside class is not a good idea, but I want to do it so because of some reasons.

My code is as below:

config.php

$CONFIG = array();
// ...
$CONFIG["DATABASE"] = array();
$CONFIG["DATABASE"]["USERNAME"] = "user";
$CONFIG["DATABASE"]["PASSWORD"] = "pass";
$CONFIG["DATABASE"]["HOSTNAME"] = "127.0.0.1";
$CONFIG["DATABASE"]["DATABASE"] = "my_db";
// ...

database.php

require('config.php');

class Database
{
    protected   $m_Link;
    private     $m_User;
    private     $m_Pass;
    private     $m_Host;
    private     $m_Data;
    private     $m_bConnected;

    public function __construct()
    {
        global $CONFIG;
        $this->m_User = $CONFIG["DATABASE"]["USERNAME"];
        $this->m_Pass = $CONFIG["DATABASE"]["PASSWORD"];
        $this->m_Host = $CONFIG["DATABASE"]["HOSTNAME"];
        $this->m_Data = $CONFIG["DATABASE"]["DATABASE"];
        $this->m_bConnected = false;
        $this->Connect();
    }

    // ...
};

There is no error given (except for the failed database connection notification).

I can't access to the array elements. For instance $CONFIG["DATABASE"]["USERNAME"] returns an empty value even though it was initialized with "user" string in the config.php.

How should I modify my code so that it can this global array can be accessible inside the class constructor?

(Note: PHP version is 5.3.0)

3
  • "(Note: PHP version is 5.3.0)" -- any chances you've a namespace declaration somewhere too? Commented May 31, 2011 at 8:51
  • @Denis: No, I didn't do namespace declaration anywhere. Commented May 31, 2011 at 8:59
  • Your "some reasons" (whatever they may be) for wanting to do this are wrong. In addition, you should not call connect() in the ctor, because that is doing work. Call connect when you need to connect and not when the object is created. It doesnt have to be connected to be in a valid state. Commented May 31, 2011 at 8:59

3 Answers 3

2

Your code looks right, so I think you should just debug it. Try to output $CONFIG before creating instance of Database class, $CONFIG may be redefined/changed somewhere in your code. And don't just check one value in array - output whole array with var_dump/print_r.

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

Comments

1

Instead of

$CONFIG = array();

use

$GLOBALS['CONFIG'] = array();

Comments

0

I think some how global is not working in __construct(). I am not sure if it is a bug or it is designed as it is.

For code

<?php
class Test {
    public $value;

    function __construct() {
        global $value;
        $value = "I am a test.";
    }
}

$test = new Test();
echo $test->value;

You will see nothing when above php runs.

However, if you do not use global, but use $this->value, everything works fine.

<?php
class Test {
    public $value;

    function __construct() {
        $this->value = "I am a test.";
    }
}

$test = new Test();
echo $test->value;

If you insist to get a reason. I think maybe __construct() is design to initialize the properties. Some code like $this->value = $value is using a lot in __construct(). So maybe the php designer thinks it is not good practice to use global in __construct(). However. I can not find a word mentioned it in php manual after all.

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.