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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:30:58+00:00 2026-05-25T09:30:58+00:00

For years I have used global $var,$var2,…,$varn for methods in my application. I’ve used

  • 0

For years I have used global $var,$var2,...,$varn for methods in my application. I’ve used them for two main implementations:

Getting an already set class (such as DB connection), and passing info to functions that display to page.

Example:

$output['header']['log_out'] = "Log Out";
function showPage(){
     global $db, $output;
     $db = ( isset( $db ) ) ? $db : new Database();
     $output['header']['title'] = $db->getConfig( 'siteTitle' );
     require( 'myHTMLPage.html' );
     exit();
}

There are, however, performance and security ramifications of doing it like this.

What alternative practice can I use that will maintain my functionality but improve design, performance, and/or security?

This is the first question I’ve ever asked on SO, so if you need clarifications please comment!

  • 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-25T09:30:58+00:00Added an answer on May 25, 2026 at 9:30 am

    The alternative is called dependency injection. In a nutshell it means that you pass the data a function/class/object requires as parameters.

    function showPage(Database $db, array &$output) {
        ...
    }
    
    
    $output['header']['log_out'] = "Log Out";
    $db = new Database;
    
    showPage($db, $output);
    

    This is better for a number of reasons:

    • localizing/encapsulating/namespacing functionality (the function body has no implicit dependencies to the outside world anymore and vice versa, you can now rewrite either part without needing to rewrite the other as long as the function call doesn’t change)
    • allows unit testing, since you can test functions in isolation without needing to setup a specific outside world
    • it’s clear what a function is going to do to your code just by looking at the signature
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

10 years have ellapsed since I used COM/OLE, and I forget 90% of them.
We have used Infragistics controls in our applications for years. However, we have always
I have used the following code to display the previous and after years of
I haven't used a statically typed language in many years and have set myself
I've used Emacs for years on Linux, and I have lots of personally useful
For many years I have used the old SAP DCOM Connector to communicate between
We have used for 8 years an ActiveX created in Delphi and we are
I have used MS Money for several years now and due to my coding
At work I have used Delphi 6 & 7 Enterprise for several years. A
I have used OO programming languages and techniques years ago (primarily on C++) but

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.