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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:37:01+00:00 2026-06-10T10:37:01+00:00

Global variables are generally considered bad practice unless absolutely necessary, so I am looking

  • 0

Global variables are generally considered bad practice unless absolutely necessary, so I am looking at alternative ways to achieve some functionality.

We are currently working on a CMS system in PHP, and wish to allow other developers to write modules easily for the CMS. We have defined a CMS class as follows:

class CMS
{
    public $version;
    public $sitename;
    public $siteurl;
    public $tbl_prefix;

    function __construct()
    { 

    }

    function RegisterModule()
    {

    }
}

Now, we would like to generate an instance of CMS, and make it accessible to the developer (and all modules and core files) through a variable: $_cms. Since we are not able to use constant variables for objects in PHP, and using a Singleton pattern provides clumsy syntax to access the instance (i.e. CMS::GetInstance()->RegisterModule()), is there any way to achieve what we want without defining a global variable containing an instance of CMS?

Ideally, we are looking for something like the following syntax:

_cms->RegisterModule(); or $_cms->RegisterModule();, with the former being preferred.

  • 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-06-10T10:37:02+00:00Added an answer on June 10, 2026 at 10:37 am

    Require plugins to be coded as classes, which either extend the CMS class or accept it as a parameter to their constructor. That’s also known as Dependency Injection and is just about the cleanest interface you can get.

    class MyPlugin {
    
        public function __construct(CMS $cms) {
            ...
        }
    
    }
    

    Requiring plugins to be classes also makes it really clean and easy to include them from your app, since they’re self-contained and can easily follow specific naming and interface conventions.

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

Sidebar

Related Questions

Possible Duplicate: Are global variables in PHP considered bad practice? If so, why? global
I've generally heard that using global variables is a bad practice when programming. So
I generally live by the rule that Global variables / functions are evil and
I recently had an issue with some global variables deleted by the system. From
I understand that global variables should be avoided generally. As a beginner, I'm trying
Possible Duplicate: Global Variables in Cocoa/Objective-C? problem with declare a global variable in objective
The discussion around global variables and their misuse seems to hold a certain dogmatic
I always hear using global variables are dangerous. Does this apply to Drupal? Take
I'm using global variables to pass around a response from a AJAX call: window.response
Possible Duplicate: Global variables in R ad the beginning of the file I wrote:

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.