5

Is there a way to access one instance of a class inside functions in PHP? Like this:

include("class.php");
$bla=new Classname();

function aaa(){
    $bla->DoSomething();  //Doesn't work.
}

$bla->DoSomething();  //Works.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MilMike
  • 12,571
  • 15
  • 65
  • 82

5 Answers5

9

Use global:

function aaa() {
    global $bla;
    $bla->DoSomething();
}

Works on all variables, not just classes.

Tatu Ulmanen
  • 123,288
  • 34
  • 187
  • 185
  • 1
    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 07 '11 at 19:45
9

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();
}
wimvds
  • 12,790
  • 2
  • 41
  • 42
  • but what if he wants to have more than one instance of that class? 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: http://code.google.com/p/google-singleton-detector/wiki/WhySingletonsAreControversial – tDo Feb 11 '10 at 20:46
  • 1
    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 12 '10 at 08:07
2

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.

tDo
  • 528
  • 3
  • 5
1

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();
}
Justin Johnson
  • 30,978
  • 7
  • 65
  • 89
1

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.

Veeti
  • 5,270
  • 3
  • 31
  • 37