Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5960259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:48:06+00:00 2026-05-22T18:48:06+00:00

My aim is to retrieve some data from a global array which is defined

  • 0

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)

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:48:07+00:00Added an answer on May 22, 2026 at 6:48 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When developing a new web based application which version of html should you aim
I started doing a new project in PHP / MySql . The Aim of
My Aim: To be able to add/amend the content inside my content editable user
My aim is to read this xml file stream: <?xml version=1.0 encoding=utf-16?> <events> <header>
It seems some web architects aim to have a stateless web application. Does that
My Aim: Sending an email (e.g. support request, error report) out of a C#
Does anyone know of any existing projects that aim to port Android's Java VM
SVN and CVS are two very different systems that aim to address the same
I'm trying to find any JavaScript frameworks whose sole aim is to standardize the
I am developing a system as an aid to musicians performing transcription. The aim

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.