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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:36:03+00:00 2026-05-19T03:36:03+00:00

I just started switching my project form the mysql to PDO. In my project

  • 0

I just started switching my project form the mysql to PDO. In my project a new PDO Object is created more or less right a the beginning of the programm.

$dbh_pdo = new PDO("mysql:host=$db_url;dbname=$db_database_name", $db_user, $db_password);

Now I would like to use this handler (is that the correct name?) in some functions and classes. Is there a way to make objects global just like variables or am I trying something unspeakably stupid, because I couldn’t find anything when searching the web …

  • 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-19T03:36:03+00:00Added an answer on May 19, 2026 at 3:36 am

    Yes, you can make objects global just like any other variable:

    $pdo = new PDO('something');
    function foo() {
       global $pdo;
       $pdo->prepare('...');
    }
    

    You may also want to check out the Singleton pattern, which basically is a global, OO-style.

    That being said, I’d recommend you not to use globals. They can be a pain when debugging and testing, because it’s hard to tell who modified/used/accessed it because everything can. Their usage is generally considered a bad practice. Consider reviewing your design a little bit.

    I don’t know how your application looks like, but say you were doing this:

    class TableCreator {
       public function createFromId($id) {
           global $pdo;
           $stmt = $pdo->prepare('SELECT * FROM mytable WHERE id = ?');
           $stmt->execute(array($id));
           $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
           foreach ($rows as $row) {
               // do stuff
           }
       }
    }
    

    You should do that instead:

    class TableCreator {
       protected $pdo;
    
       public function __construct(PDO $pdo) {
           $this->pdo = $pdo;
       }
    
       public function createFromId($id) {
           $stmt = $this->pdo->prepare('SELECT * FROM mytable WHERE id = ?');
           $stmt->execute(array($id));
           $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
           foreach ($rows as $row) {
               // do stuff
           }
       }
    }
    

    Since the TableCreator class here requires a PDO object to work properly, it makes perfect sense to pass one to it when creating an instance.

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

Sidebar

Related Questions

Right guys, I have just started getting into Xcode 4, with the intention to
So I just started switching over from SDL to OpenGL today and I'm having
I just recently started with object oriented programming, using java. Before I was programming
I created a new Branch before I started dev on something experimental. I usually
I'm just getting started switching from flash to flex for the better components. I
I recently started switching my project from Flex SDK 3.5 to 4.5.1, but my
I just started switching my old DTD over to XSD when I found out
Just started getting a bunch of errors on our C# .Net app that seemed
Just started to get my feet wet with C# and .NET, liking it so
Just started with JQuery and I've managed to pass a parameter using POST, firebug

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.