Global PHP class in functions?

If I interpret your question correctly, then the proper way to do this is create a singleton class class Singleton { private static $instance; private function __construct() {} private function __clone() {} public static function getInstance() { if (!Singleton::$instance instanceof self) { Singleton::$instance = new self(); } return Singleton::$instance; } public function DoSomething() { ... } } You would call this in your function as follows : function xxx() { Singleton::getInstance()->DoSomething(); }.

If I interpret your question correctly, then the proper way to do this is create a singleton class. Class Singleton { private static $instance; private function __construct() {} private function __clone() {} public static function getInstance() { if (!Singleton::$instance instanceof self) { Singleton::$instance = new self(); } return Singleton::$instance; } public function DoSomething() { ... } } You would call this in your function as follows : function xxx() { Singleton::getInstance()->DoSomething(); }.

A singleton is unnecessary here. – GSto Feb 11 '10 at 20:35 Read his question again. He specifically asks "is there a way to access one instance of a class inside functions in php" – wimvds Feb 11 '10 at 20:38 1 And like I already said in the answer, just a small link you should follow and decide for yourself if singleton is really the right way: code.google.

Com/p/google-singleton-detector/wiki/… – tDo Feb 11 '10 at 20:46 True, he should indeed first look at all the options and decide whether or not the singleton approach is the way to go, but that exercise is left to the poster of the question. It's not something I can decide for him... – wimvds Feb 11 '10 at 8:07.

Use global: function aaa() { global $bla; $bla->DoSomething(); } Works on all variables, not just classes.

This is the most direct solution and will at least make your slightly-coupled code work until the more elegant code is written. – grantwparks Aug 7 at 19:45.

The cleaner way would be to pass the instance by reference to the given class and then access it. Another way would be to use a singleton pattern, though many argue that it's not really better than a global.

As already answered, you could use a global variable to store the class instance, but it sounds to me like you should consider using something like the Singleton pattern instead for a cleaner implementation. You can find a simple example of a singleton class here.

If you want to enforce using only a single instance of a class throughout your application, you should use a singleton, not a global. You could do something like this: class Classname { private static $instance; private function __construct() {...} public function doSomething() {...} // The singleton method public static function singleton() { if (!isset(self::$instance) ) { self::$instance = new self; } return self::$instance; } private function __clone() { /* do nothing here*/ } } function aaa() { Classname::getInstance()->doSomething(); } See PHP Pattens for more implementation details.

Sometimes a variable available in global scope is not accessible via the 'global' keyword or the $GLOBALS superglobal array. I have not been able to replicate it in original code, but it occurs when a script is run under PHPUnit. PHPUnit provides a variable "$filename" that reflects the name of the file loaded on its command line.

This is available in global scope, but not in object scope. For example, the following phpUnit script (call it GlobalScope. Print "Method scope GLOBALSFILENAME ".

If you run this script via "phpunit GlobalScope. Global scope FILENAME /home/ktyler/GlobalScope. PHPUnit 3.4.5 by Sebastian Bergmann.

Print "Method scope GLOBALSFILENAME ". By doing this, both "global" and $GLOBALS work! I don't know what it is that PHPUnit does (I know it uses Reflection) that causes a globally available variable to be implicitly unavailable via "global" or $GLOBALS.

But there it is.

If I interpret your question correctly, then the proper way to do this is create a singleton class.

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