Can I make a variable globally visible without having to declare it global in every single PHP class's constructor?

☆樱花仙子☆ 提交于 2019-12-01 02:51:27

问题


I have a database class, which an instance is declared in the main index.php as

$db = new Database();

Is there a way for the $db variable to be globally recognized in all other classes without having to declare

global $db;

in the constructor of each class?


回答1:


No. You have to declare Global $db in the constructor of every class.

or you can use the Global array: $_GLOBALS['vars'];

The only way to get around this is to use a static class to wrap it, called the Singleton Method (See Here for an explanation). But this is very bad practice.

  class myClass
   {
    static $class = false;
    static function get_connection()
    {
        if(self::$class == false)
        {
            self::$class = new myClass;
        }
        else
        {
            return self::$class;
        }
    }
    // Then create regular class functions.
   }

The singleton method was created to make sure there was only one instance of any class. But, because people use it as a way to shortcut globalling, it becomes known as lazy/bad programming.

StackOverflow Knowledge
How to Avoid Using PHP Global Objects
Share Variables Between Functions in PHP Without Using Globals
Making a Global Variable Accessible For Every Function inside a Class
Global or Singleton for Database Connection




回答2:


I do it a little different. I usually have a global application object (App). Within that object I do some basic initialization like creating my db objects, caching objects, etc.

I also have a global function that returns the App object....thus (application object definition not shown):

define('APPLICATION_ID', 'myApplication');

${APPLICATION_ID} = new App;

function app() {
    return $GLOBALS[APPLICATION_ID];
}

So then I can use something like the following anywhere in any code to reference objects within the global object:

app()->db->read($statement);
app()->cache->get($cacheKey);
app()->debug->set($message);
app()->user->getInfo();

It's not perfect but I find it to make things easier in many circumstances.




回答3:


you could use

$GLOBALS['db']->doStuff();

or alternatively using some kind of singleton access method

Database::getInstance()->doStuff();



回答4:


Why not create a class that contains the global $db; in it's constructor, then extend all other classes from this?




回答5:


You could use a Registry class to store and retrieve your Database instance.

class Registry
{
    protected static $_data = array();

    public static function set($key, $value)
    {
        self::$_data[$key] = $value;
    }

    public static function get($key, $default = null)
    {
        if (array_key_exists($key, self::$_data)) {
            return self::$_data[$key];
        }

        return $default;
    }
}

Registry::set('db', new Database());
$db = Registry::get('db');


来源:https://stackoverflow.com/questions/1234445/can-i-make-a-variable-globally-visible-without-having-to-declare-it-global-in-ev

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!