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

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.

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.

But I included it because it 'technically' can be used. – Dave Hunt Aug 5 '09 at 17:00 I updated with some other links you might look into. I actually do support using Singleton for a database class, but you still should global it.

This singleton method still doesn't make the $db variable global within other classes. – Dave Hunt Aug 5 '09 at 17:21.

You could use $GLOBALS'db'->doStuff(); or alternatively using some kind of singleton access method Database::getInstance()->doStuff().

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').

Your registry class is not, and adds no extra functionality. Some sort of Options class might be useful for storing app options, but even that could probably replaced by an associative array. – Samir Talwar Aug 5 '09 at 21:05 This basic construct adds no extra functionality.

But you're free to add. For example debugging or logging features. Performance is no argument, sorry ... we're in OO context here.

And as soon as you use third-party software you want to encapsulate your data. See Zend_Registry in Zend Framework. – Philippe Gerber Aug 5 '09 at 22:13.

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 $GLOBALSAPPLICATION_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.

To import all global variables incl. The problem is with the superglobals here. You get the parameter variable names with get_defined_vars.

That's also the reason why the opposite is less tricky, get_defined_vars does not return the superglobals, only the local variables. The global creates a reference to the variable of the global scope, so it's actually a local variable that is an alias to the global variable with the same name. Note that like globals the $GLOBALS superglobal array contains references to the global variables as well, so this creates references here as well.

This is especially needed if you import via global or &$GLOBALS... or the extract like above.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions