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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:56:35+00:00 2026-05-22T19:56:35+00:00

I have several classes in an application that I am currently building, and I

  • 0

I have several classes in an application that I am currently building, and I want to have one access some of the other’s member functions but i can’t seem to do it.

The first class is called MySQLDB:

class MySQLDB{
public $connection;

function __construct(){
//connects to database
}

function login($username, $password){
//queries database...
}
}

Then I have a class called Session:

class Session{
//variables
//constructor

function processlogin($username, $password){
$database->login($username, $password);
}

Then after this I have two class declarations:

$database = new MySQLDB();
$session = new Session();

No matter where i put these statements in relation to the classes I still get the same error:

PHP Notice:  Undefined variable: database in C:\inetpub\wwwroot\cmu\include\session.php on line 52
PHP Fatal error:  Call to a member function login() on a non-object in C:\inetpub\wwwroot\cmu\include\session.php on line 52

I have seen some suggestions that would suggest putting the new database object inside the Session class declaration but I want to avoid doing so because I use the database class several other places in the code and I don’t want to open up multiple connections to the database.

  • 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-22T19:56:36+00:00Added an answer on May 22, 2026 at 7:56 pm

    since you want to have acces on a globally set variable, you can either gain access to it with global:

    function processlogin($username, $password){
        global $database;
        $database->login($username, $password);
    }
    

    or use the variable as a parameter for the contructor and remember the database object reference in the class Session:

    class Session{
    
        private $database;
    
        function __construct($database){
            $this->$database = $database;
        }
    
        function processlogin($username, $password){
            $this->database->login($username, $password);
        }
    
    }
    

    and then you call:

    $database = new MySQLDB();
    $session = new Session($database);
    

    this comes in handy, if you use more functions afterwords, that also need access to the database object.

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

Sidebar

Related Questions

Currently i have an application that reads and writes several properties from one or
I have a keystore file that I want available to several classes in my
I have an application that uses several different Java classes and would like to
I have an application that has several classes used for storing application-wide settings (locations
I have several classes, that all derives from SuperClass. When the classes are created,
I have several classes, and want to work with their collections like with a
If I have interface IFoo, and have several classes that implement it, what is
This is for a web project so i have several classes that inherit from
I have several service classes that have static methods and offer a service to
I have several small classes that are in a single file in /app/models, similar

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.