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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:41:32+00:00 2026-05-13T09:41:32+00:00

Should have asked someone this a long time ago. What is the best way

  • 0

Should have asked someone this a long time ago.

What is the best way to use other classes within another class?

For instance, lets say I have an application class:

class Application
{
    public function displayVar() {
        echo 'hello world';
    }
}

and a database class

class Database
{
    // connects to db on construct
    public function query() {
        // queries db
    }
}

now, i want to add a function to my application class that uses a function from the db class

class Application
{
    public function displayVar() {
        echo 'hello world';
    }
    public function getVar() {
        global $db;
        $sql = foo;
        $db->query($sql);
    }
}

so then I have

$db = new Database();
$app = new Application();
$app->getVar('var');

Is there a better way of doing this? Really what I am looking for is the standard way of doing it, not another way of rigging it.

  • 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-13T09:41:32+00:00Added an answer on May 13, 2026 at 9:41 am

    There are a couple of ways of doing that. Global variables is certainly one way and the most looked down upon too. You can create a Singleton and all other classes that need database access would call upon this singleton.

    final class Database {
        private static $connection;
    
        public static function getInstance() {
            if(self::$connection == NULL) {
                self::$connection = // init your database connection
            }
            return self::$connection;
        }
    }
    

    And use this database connection object in whatever class needs it.

    class Application {
        public function displayVar() {
            echo 'hello world';
        }
        public function getVar() {
            $db = Database::getInstance();
            $sql = foo;
            $db->query($sql);
        }
    }
    

    This is all well for a start and a great step beyond using global variables, but you can do better with Dependency Injection. Dependency Injection is a simple concept that if a class has any external dependencies, such as the database connection in your example, you explicitly pass those to the needy class in its constructor or a method. So the new code would look something like Jonathan’s solution. A major advantage of using dependency injection is in unit testing, where you can easily replace this actual database object with a mock object and pass it to whoever needs it.

    class Application {
        private $db;
    
        public function __construct(Database $db) {
            $this->db = $db;
        }
    
        public function displayVar() {
            echo 'hello world';
        }
    
        public function getVar() {
            $sql = foo;
            $this->db->query($sql);
        }
    }
    

    For smaller projects, you can easily do it yourself. For large projects, there are various DI frameworks available for PHP

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

Sidebar

Related Questions

Recently I have asked a question about what I should use to create self-contained
I asked this question over Security site, and people there suggested I should have
I should have asked this in Facebook developer forum instead, but somehow I can't
Over a year ago someone asked this question: Execute .sql files that are used
OK, this should really be asked to someone from Google, but I just want
I have asked What should i know about search engine crawling? Now i would
Sorry if this question has all ready been asked, maybe someone can point me
Someone here asked: Linq-To-Sql enables calling SPs. If this SP executes an update/delete/insert, do
Possible Duplicate: How write a recursive print program I have already asked this question,
I'm sure this would have been asked before, but I'm very confused! Say I

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.