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

  • Home
  • SEARCH
  • 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 1080533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:02:58+00:00 2026-05-16T22:02:58+00:00

I have a class that I am using all over the place in my

  • 0

I have a class that I am using all over the place in my code. It contains settings and other core functionality. Here’s what I’m doing right now to use the class.

$settings = new Settings();
$settings->loadSettings();

Then, when I need to the code in some random function in another class, I do something like this:

function abc() {
    global $settings;
    $variable = $settings->a;
}

I’m sick and tired of randomly calling global $settings all over the place to pull in that settings object. I don’t want to use the $_GLOBALS array (I don’t know why, I just don’t want to).

I’m thinking I want to switch to have a static class variable called $settings inside of settings. The code would look like this:

Settings::$settings = new Settings();
Settings::$settings->loadSettings();

Then, whenever I want to use it, I never have to worry about sucking it in via the global operator:

function abc() {
    $variable = Settings::$settings->a;
}

Good idea or bad idea?

  • 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-16T22:02:59+00:00Added an answer on May 16, 2026 at 10:02 pm

    Well it’s probably an improvement on globals, because it solves all the ugly scoping issues that globals cause. Getting rid of the global operator is generally a good thing! What you are doing is not dissimilar to the singleton pattern, though it’s considerably simpler. (See the “Singleton” section at http://php.net/manual/en/language.oop5.patterns.php for more information on the pattern.) Your solution is almost certainly fine for your purposes.

    On the other hand, there may be better ways of achieving the same thing that decouple your code more. That is to say, each class becomes more capable of being used in another project without significant recoding. One way to do this would be to “inject” the settings object into each class:

    class ABC {
        private $settings;
    
        public function __construct($settings) {
            $this->settings = $settings;
        }
    
        public function someMethod() {
            $variable = $this->settings->a;
        }
    }
    

    This would be more work, but may improve the re-usability of your code. You could then, for example, write a different settings class for every project but use the same ABC class.

    This process, where you “inject” an object into another object that depends on it, is called dependency injection. There are other, more complex ways of doing this, including complex containers. See http://fabien.potencier.org/article/11/what-is-dependency-injection for an interesting set of tutorials on the subject. They’re probably incidental to your current needs, but may help either now or in the future.

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

Sidebar

Related Questions

This is in C#, I have a class that I am using from some
I have a class that I wish to expose as a remote service using
I have a class that subclasses NSMutableArray. I init it using: MyClass class =
I have a small class that displays an alert via Javascript. I am using
Using C# .NET 2.0, I have a composite data class that does have the
I have a class that creates several IDisposable objects, all of these objects are
I have class method that returns a list of employees that I can iterate
I have a class that I want to use to store properties for another
I have a class that map objects to objects, but unlike dictionary it maps
I have a class that looks like this public class SomeClass { public SomeChildClass[]

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.