Page 1 of 1

Globally acces an object...

Posted: Sun Jul 20, 2008 6:29 pm
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();
  }
 
}

Re: Globally acces an object...

Posted: Sun Jul 20, 2008 6:58 pm
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];
        }
    }
}
 

Re: Globally acces an object...

Posted: Mon Jul 21, 2008 2:45 pm
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! ;) )

Re: Globally acces an object...

Posted: Mon Jul 21, 2008 3:24 pm
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.

Re: Globally acces an object...

Posted: Mon Jul 21, 2008 6:13 pm
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

Re: Globally acces an object...

Posted: Mon Jul 21, 2008 6:23 pm
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.