Globally acces an object...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Inkyskin
Forum Contributor
Posts: 282
Joined: Mon Nov 19, 2007 10:15 am
Location: UK

Globally acces an object...

Post by Inkyskin »

Hi all, is it possible to globally access an object somehow? I have three files for example:

Sql.class.php

Code: Select all

class Sql {
 
  protected $db_hostname;
 
  public function __construct($hostname)
  {
    $this->db_hostname = $hostname;
  }
  
  public function return_hostname()
  {
    return $this->db_hostname;
  }
 
}
Test.class.php

Code: Select all

class Test {
 
  public $sql;
  
  public function __construct()
  {
    global $sql;
    $this->sql = $sql;
  }
 
  public function test_func()
  {
    return $this->sql->return_hostname();
  }
 
}
test.php

Code: Select all

include '../libs/Sql.class.php';
include '../libs/Test.class.php';
 
$sql = new Sql('localhost');
 
$test = new Test();
echo $test->test_func();
As you can see in Test.class.php, I'm having to globally declare $sql, and then reference it with $this->sql. Is there a way to globally define the object in one place, so I can just call $sql within the class without it having to be assigned anywhere? (Like this):

Code: Select all

class Test {
 
  public function test_func()
  {
    return $sql->return_hostname();
  }
 
}
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Re: Globally acces an object...

Post by shiznatix »

you could use the $GLOBAL array or just use the global keyword but I don't think thats what you want to do. What you want is a little baby called a singleton. Basically it is a static class that will hold object instances for you. Very helpful little thing. Quick example:

Code: Select all

 
<?php
 
/**
 This class stores all of the instances of classes from the MVC that
 are loaded. We want to make sure that we keep all of those classes
 in one place.
 
 This singleton is only for the classes from the MVC pages
 */
class singleton
{
    static private $objects = array();//an array of the objectes stored here
 
        /**
         Keep this method private to make sure no instance of this class is made
         */
    private function __construct()
    {
    }
 
        /**
         Method to get an object that is stored here. If the object does not
         exist, it will be created and then returned.
 
     Usage: singleton::get_object('icecream');
 
         @param $name the name of the class
         @return      the object
         */
    static public function get_object($name)
    {
        if (isset(self::$objects[$name]))
        {
            return self::$objects[$name];
        }
        else
    {
            self::$objects[$name] = new $name;
            return self::$objects[$name];
        }
    }
}
 
User avatar
Inkyskin
Forum Contributor
Posts: 282
Joined: Mon Nov 19, 2007 10:15 am
Location: UK

Re: Globally acces an object...

Post by Inkyskin »

Hi, I think I have explained a little wrongly... I don't mind multiple instances of the class at all, the problem is I don't want to assign the object every time I use it in a class, this means constantly having to create the object/instance somewhere, and then call it using $this->sql, rather than just $sql, without having to create the object at all...

The singleton style means I still have to assign a variable within the class the instance before I can use it.

Basically, I want to create the object from outside the class, and then access it within the class without having to define it first.

(Although I now know about singletons, so that's all good! ;) )
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Globally acces an object...

Post by Christopher »

I think it would be better to do:

Test.class.php

Code: Select all

class Test {
 
  public $sql;
  
  public function __construct($sql)
  {
    $this->sql = $sql;
  }
 
  public function test_func()
  {
    return $this->sql->return_hostname();
  }
 
}
test.php

Code: Select all

include '../libs/Sql.class.php';
include '../libs/Test.class.php';
 
$sql = new Sql('localhost');
 
$test = new Test($sql);
echo $test->test_func();
I would probably rename "Sql" to something like "Db" or "Connection" too.
(#10850)
User avatar
Inkyskin
Forum Contributor
Posts: 282
Joined: Mon Nov 19, 2007 10:15 am
Location: UK

Re: Globally acces an object...

Post by Inkyskin »

It's not actually named sql, that was just for here ;)

Looks like that will be the best route to take, thanks :) I hope in v6 they implement something like a truly global (like define()) object system, I would love that :D
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Globally acces an object...

Post by Christopher »

Inkyskin wrote:I hope in v6 they implement something like a truly global (like define()) object system, I would love that :D
I doubt there will be "truly global" objects like you are thinking of. You an put your object in the $_GLOBAL superglobal array, or use a function static class call like shiznatix showed. But passing a real object in will probably be the best choice in the long run.
(#10850)
Post Reply