tags:

views:

1761

answers:

4

I'm currently creating blog system, which I hope to turn into a full CMS in the future.

There are two classes/objects that would be useful to have global access to (the mysqli database connection and a custom class which checks whether a user is logged in).

I am looking for a way to do this without using global objects, and if possible, not passing the objects to each function every time they are called.

+5  A: 

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

myClass::myFunction();

That will work anywhere in the script. You might want to read up on static classes however, and possibly using a Singleton class to create a regular class inside of a static object that can be used anywhere.

Expanded

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

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.
}

What happens is after you get the connection, using $object = myClass::get_connection(), you will be able to do anything function regularly.

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

Expanded

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

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.
    public function is_logged_in()
    {
     // This will work
     $this->test = "Hi";
     echo $this->test;
    }
}

$object = myClass::get_connection();
$object->is_logged_in();
Chacha102
Hi, thanks for your answer, is it possible to save variables in static classes. i.e. call function to check whether user is logged in, and then access this variable several times later in the page?
Nico Burns
Yes, for a static class do the same thing I did to declare the variable $class. The only thing is that you 'have' to declare every variable you use.
Chacha102
But, the code I gave you allows you to use any regular class, well, regularly. The static part simply gives you the class, instead of globaling it. I'll edit to show a little more.
Chacha102
wow. thanks a lot, I would press the up arrow twice if I could. Am I right in thinking that the get_connection() function retrieves the previous created object? (or creates the object if its hasn't already been created)
Nico Burns
Exactly! The entire Static class is simply a container for the created class.
Chacha102
A static class is just another type of global object.
troelskn
Aha! I have the idea now. One more question, the "public" bit of "public function is_logged_in()", is that specific to a static class, or is that just how you declare all of your functions?
Nico Burns
The public part is simply something I put in by default. It has to do with PHP5 and visibility. Public means anyone, in or outside of the class can see it. Protected and Private are other visibility settings.
Chacha102
-1, classes are global symbols too, so static public members of classes qualify as global variables. It's just the syntax that is different.
Ionuț G. Stan
You don't have to global it everytime that you create a new file or class, which was the base problem the OP was trying to fix.
Chacha102
A: 

Well, if you already have some object by which you refer to the blog system, you can compose these objects into that, so that they're $blog->db() and $blog->auth() or whatever.

chaos
I could, but the objects will also need to accessed outside the blog class as well.
Nico Burns
A: 

You can take a look at the Design Pattern called "Singleton", and/or more specifically "Registry" (see this, for example)

If you are using a Framework, some have classes designed for that, like Zend_Registry

Pascal MARTIN
Isn't singleton more or less like a global variable?
eed3si9n
I would say Registry is more or less a global variable (except you don't pollute the global namespace). Singleton is more a way of ensuring you only have one instance of something (a DB connection, for example)
Pascal MARTIN
Registry pattern, if you coded against interface, is at least detached from implementation in dependency injection way. Whereas singleton is stuck with the implementation and if the object is stateful, external change can affect the behavior of the consumer causing "content coupling." I'm not particularly against singleton, but just wanted to make it clear that it can effectively be a global variable.
eed3si9n
+3  A: 

You could pass the currently global objects into the constructor.

<?php
  class Foo {
    protected $m_db;
    function __construct($a_db) {
      $this->m_db = $a_db;
    }
  }
?>
eed3si9n