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 7635119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:21:37+00:00 2026-05-31T07:21:37+00:00

As the title says there is a problem accessing variable (associative array) inside class

  • 0

As the title says there is a problem accessing variable (associative array) inside class from included file. Here is the source code both class and include file:

require("applications/cw_database.php");
require("config/dbConfig.php");
require("config/appConfig.php");

class APP_ASSESMENTS
{
    private $dbObj;
    private $DisplayOutput  = "";

    public function __construct($PageParams)
    {
        try
        {
            $dbObj = new CW_DB($dbConfig['hostname'],$dbConfig['username'],$dbConfig['password'],$dbConfig['name'],$dbConfig['port']);
        } catch (Exception $e) {
            throw new ErrorException($e);
        }
    }
...

The other part has nothing to do with $dbConfig.
Also this is the included file (config/dbConfig.php):

    /*
    Testing configuration for MySQL database
*/
$dbConfig['username']   = "phpcoursework";  // changed on demand
$dbConfig['password']   = "phpcoursework";  // changed on demand
$dbConfig['hostname']   = "localhost";      // changed on demand
$dbConfig['name']       = "students";       // changed on demand
$dbConfig['port']       = 3306;             // default for MySQL
  • 1 1 Answer
  • 0 Views
  • 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-31T07:21:38+00:00Added an answer on May 31, 2026 at 7:21 am

    First, $dbObj will not automatically assume class member scope, it will create a local copy of CW_DB and discard it when __construct returns. You need to explicitly reference the property;

    $this->dbObj = ...
    

    Anyway, global state using global as suggested by others will “work“, but if you’re using OOP practices you’re best not to do that. You can actually return from an include(), so an option would be to do the following:

    // your config file dbConfig.php
    return array(
        'username' => "phpcoursework",
        'password' => "phpcoursework",
        'hostname' => "localhost",
        'name' => "students",
        'port' => 3306,
    );
    

    And inject it into the object, via constructor or method (here’s constructor)

    class APP_ASSESMENTS
    {
    
        private $dbObj;
    
        public function __construct($dbConfig, $PageParams)
        {
            $dbObj = new CW_DB($dbConfig['hostname'], $dbConfig['username'],
                $dbConfig['password'], $dbConfig['name'], $dbConfig['port']);
            // ...
        }
    
    }
    
    // include() here, will actually return the array from the config file
    $appAssesments = new \APP_ASSESMENTS(include('dbConfig.php'), $PageParams);
    

    It would be recommended that you go another level up: instead, inject the database object itself, taking the dependency out of your APP_ASSESSMENTS class.

    (Also, PascalCase is the typical convention of class naming, such as AppAssessments and CwDb)


    $dbObj = new CwDb(include('dbConfig.php'));
    $appAssessments = new AppAssessments($dbObj, $etc, $etc);
    

    This simple change allows you to remove the dependency from AppAssessments on CwDb. That way, if you extend CwDb for some reason, you can just pass in an instance of the extended class without having to change any code in AppAssessments

    You can change the AppAssessments constructor like so:

    public function __construct(CwDb $db, $etc, $etc){
        $this->db = $db;
        // ...
    }
    

    This takes advantage of PHPs (limited, albeit still useful) type-hinting, ensuring the first argument is always of the correct type.

    This plays into part of the open/closed principle: classes should be open to extension but closed for modification.

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

Sidebar

Related Questions

As the title says: Is there a difference between $str == '' and strlen($str)
so basically like the title says is there a way to use NSSortDescriptor to
As the title says, is there a way to make the maximum value of
What the title says. Is there a way to change intellisense settings (intellisense box
As the title says, is there any Scala library that exports functions to convert,
As the title says, is there any predefined time set in the operating system
What the title says. I want to know if there something in the JDK
Exactly what my title says is the problem Im having right now. Im checking
As the title says, I want the ‘select file’ dialog to open when a
Here is my regex: $table_pattern = /<TABLE.*?>(.*?)<\/TABLE>/is; Like the title says, it works in

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.