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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:13:10+00:00 2026-05-13T10:13:10+00:00

after reading this question i wonder if someone can help me understand how to

  • 0

after reading this question i wonder if someone can help me understand how to implement correctly Dependency Injection with these PHP Classes:

class DBClass
{
    private $mMysqli;
    function  __construct(mysqli $database)
    {
        $this->mMysqli=$database;
    }
    function __destruct()
    {
        $this->mMysqli->close();
    }

    public function listUsers()
    {
        $query='SELECT * FROM Utente;';
        $resultset=$this->mMysqli->query($query);
        while($row = $resultset->fetch_array(MYSQLI_ASSOC)) {
            echo $row['username'];
            echo $row['pwd'];
            echo "<br />\n";
        }

    }

    public function runQuery($query)
    {
        return $resultset=$this->mMysqli->query($query);
    }

    public function getConnection()
    {
        return $this->mMysqli;
    }
}

Session class:

class Session
{
    private $_session;
    public $maxTime;
    private $database;

    public function __construct(DBClass $database)
    {
        $this->database=$database;
        $this->maxTime['access'] = time();
        $this->maxTime['gc'] = get_cfg_var('session.gc_maxlifetime');
        session_set_save_handler(array($this,'_open'),
                array($this,'_close'),
                array($this,'_read'),
                array($this,'_write'),
                array($this,'_destroy'),
                array($this,'_clean')
                );
        register_shutdown_function('session_write_close');
        session_start();
        ...
     }
}   

User Class (for details about the currently logged in user):

class User
{
    private $username;
    private $role;
    private $session;

    function __construct($session)
    {
        $this->session=$session;
        ...
    }
}

Externally:

$connection=new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
$database=new DBClass($connection);
$session=new Session($database);
$user=new User($session);

Is this the proper way?

  • 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-13T10:13:11+00:00Added an answer on May 13, 2026 at 10:13 am

    Yes. You are now injecting the dependencies through the constructor which provides for less coupling. You can now more easily swap out these dependencies, e.g. when UnitTesting you can replace them with Mocks.

    You have still some coupling though by using the concrete DBClass as a TypeHint instead of an interface. So when you would want to use a different DBClass, it would have to be named DBClass. You could achieve more loose coupling by coding against an interface that the concrete classes have to implement instead.

    To create only single instances of a class (as asked in the comments), you could either use a Singleton (like Kevin Peno suggested) or a Factory to create and keep track of if the instance has been created yet. Or use a DI Service Container, which is similar to a Factory, yet not the same thing. It creates and manages objects for you.

    The Symfony Components library has a Dependency Injection Container with excellent documentation and introduction to the topic on how to further enhance DI this with Service Containers. The containers can also be used to limit instances. Check it out.

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

Sidebar

Related Questions

After reading and commenting on this question PHP Library for Keeping your site index
After reading this question I started to wonder: is it possible to have a
After reading this question , i saw the answer by Naveen containing a link
This seems to be an embarrassingly simple question, but, after a day of reading
After reading this question I attempted to clean out my workspace and found that
After reading this question , I've learned that denormalization is not a solution for
After reading this SO question and noting the consensus about just how evil Thread.Sleep()
I started with this question: Multiple Javascript gadgets per page . After reading up
After reading the answer for the question: JavaScript inner-functions and Performance I wonder if
After reading this question here: Passing one Dimension of a Two Dimensional Array 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.