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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:53:35+00:00 2026-05-23T18:53:35+00:00

My friend adviced me: Jesus, man! Don’t use GLOBALs. It makes your software slow.

  • 0

My friend adviced me: Jesus, man! Don't use GLOBALs. It makes your software slow.

This is my way:

// Database class
class DB extends mysqli { ... }

// create Database object
$db = new DB(...)

// My class
class A {
  function foo(){
    global $db; ## PROBLEM IS HERE

    $db->get_all(...);
  }
}

Is there any way to use $db object without make it GLOBAL? or should I stop to listen to my friend?

  • 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-23T18:53:36+00:00Added an answer on May 23, 2026 at 6:53 pm

    Sure. Just create a property for your $db object and pass it into object A, via the constructor (or create a setter method):

    class A
    {
        protected $db;
    
        public function __construct($db)
        {
            $this->db = $db;
        }
    
        function foo()
        {
            $this->db->get_all(...);
        }
    }
    
    // create your objects. inject the DB object into object A
    $db = new DB();
    $a = new A($db);
    

    Is this what you mean?

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

Sidebar

Related Questions

My friend gave me a database file: record.mdf . I copied that .mdf file
A friend show me this sample code to implement HTTP POST in C# and
My friend done this below coding for custom control <a href=javascript:__doPostBack('id','msg');>click</a> now i want
A good friend recommended this site to me, it looks really useful! I'm a
Lets say that me and my friend are trying to work on same software
I am trying to create a way a user can contact a friend via
I am trying to develop a database for a website. This website has registered
Sorry in advance if this not 'SO'-ish enough--- I have a tech writing friend
This was a question in one my friend's programming class. Q. How do you
One of my friend asking me something related to Client Software and Cloud. Java

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.