How to avoid using global PHP objects?

I am currently creating a blog system that I hope will turn into a full-fledged CMS in the future.

There are two classes / objects that would be useful for global access (a mysqli database connection and a user class that checks if the user is logged in).

I am looking for a way to do this without using global objects and, if possible, not passing objects to every function with every call.

+6
php global
Jul 18 '09 at 17:27
source share
4 answers

You can make Static objects, then you have access to them anywhere. Example:

myClass::myFunction(); 

This will work anywhere in the script. However, you can read static classes and possibly use the Singleton class to create a regular class inside a static object that you can use anywhere.

Extended

I think that what you are trying to do is very similar to what I am doing with my DB class.

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

What happens after you get the connection using $ object = myClass :: get_connection (), you can do something regularly.

 $object = myClass::get_connection(); $object->runClass(); 

Extended

Once you make these static declarations, you just need to call get_connection and assign the return value to the variable. Then the rest of the functions may have the same behavior as the class that you called with $ class = new myClass (because this is what we did). All you do is store the class variable inside a static class.

 class myClass { static $class = false; static function get_connection() { if(self::$class == false) { self::$class = new myClass; } return self::$class; } // Then create regular class functions. public function is_logged_in() { // This will work $this->test = "Hi"; echo $this->test; } } $object = myClass::get_connection(); $object->is_logged_in(); 
+13
Jul 18 '09 at 17:29
source share

You can pass the current global objects to the constructor.

 <?php class Foo { protected $m_db; function __construct($a_db) { $this->m_db = $a_db; } } ?> 
+8
Jul 18 '09 at 17:32
source share

Recently, I updated my structure in preparation for the second version of our company CMS. I unzipped a huge number of things that I did static to replace them with ordinary objects. In doing so, I created tremendous flexibility that was used to rely on how I went about and cracked kernel files. Now I use only static constructs, when the only alternative is global functions that are associated only with basic low-level functionality.

I am going to show a few lines of my bootstrap.php file (all my requests are sent through this file, but you can achieve the same result by including it at the beginning of each file) to show you what I mean. This is a pretty impressive version of what you probably use in your situation, but hopefully this idea is useful. (All of this is slightly modified.)

  //bootstrap.php ... // CONSTRUCT APPLICATION { $Database = new Databases\Mysql( Constant::get('DATABASE_HOST'), Constant::get('DATABASE_USER'), Constant::get('DATABASE_PASSWORD'), Constant::get('DATABASE_SCHEMA') ); $Registry = new Collections\Registry; $Loader = new Loaders\Base; $Debugger = new Debuggers\Dummy; // Debuggers\Console to log debugging info to JavaScript console $Application = new Applications\Base($Database, $Registry, $Loader, $Debugger); } ... 

As you can see, I have every opportunity to create my application object, which I can provide as an argument in the constructor for other objects to give them access to these "global" needs.

The database object is self explanatory. The registry object acts as a container for the object that I can get elsewhere in the application. The bootloader acts as a utility for loading other resources, such as template files. And the debugger should handle the debug output.

I can, for example, change the database class that I create, and, voila, I have a connection to the SQLite database. I can change the debugger class (as noted), and now all my debugging information will be written to my JavaScript console.

Ok, back to this problem. How do you give other objects access to all of this? You just pass it in an argument to the constructor.

 // still bootstrap.php ... // DISPATCH APPLICATION { $Router = new Routers\Http($Application); $Router->routeUri($_SERVER['REQUEST_URI']); } ... 

Not only that, but my Router (or any other object that I build with it) is also more flexible. Now I can just instantiate the application object in different ways, and my Router will behave differently accordingly.

+3
Aug 10 '13 at 4:29
source share

Well, if you already have some object with which you refer to the blog system, you can build these objects so that they are $blog->db() and $blog->auth() or something else .

+1
Jul 18 '09 at 17:30
source share



All Articles