1

In PHP, we can easy to reference an object in any classes using singleton, e.g.

$obj = SingletonClass::getInstance();

If I am not using singleton, are there any alternative?

Howard
  • 17,155
  • 33
  • 104
  • 173
  • 4
    I cannot make sense of the question. – Jon Aug 19 '14 at 09:55
  • Store it in a variable? What are you asking here? – Matt Aug 19 '14 at 09:55
  • He's asking if it's possible to check if there's already an instance of an object **without** using the `singleton pattern` - i guess – KhorneHoly Aug 19 '14 at 09:56
  • 1
    go through every single entry in the `$GLOBALS` and evaluate whether its an `INSTANCEOF ClassName` ?! – RaggaMuffin-420 Aug 19 '14 at 09:57
  • It would be interesting to know WHY you don't / can't use singleton ? – ClmentM Aug 19 '14 at 10:02
  • @KhorneHoly: How did you reach that conclusion? The sample code does not *check* anything, it *does* something. – Jon Aug 19 '14 at 10:05
  • @Jon Bad wording from me, should've been `if it's possible to GET an instance of an object if it exists, else create one, without singleton`.. like, use singleton without singleton – KhorneHoly Aug 19 '14 at 10:08
  • @KhorneHoly use a factory to produce instances, said factory then can internally store it and reuse. Like in this example: http://stackoverflow.com/a/11369679/727208 – tereško Aug 19 '14 at 16:40

3 Answers3

0

If I am not using a Singleton, are there any alternatives?

Yes, this thing is called Dependency Injection and it has been discussed million times before. Basically that means, you instantiate a class, and then pass its instance around another classes that require it. For example:

$pdo = new PDO(....);

$userGateway = new UserGateway($pdo);
$imageGateway = new ImageGateway($pdo);

As you can see, the same instance is shared across those classes.

Yang
  • 8,267
  • 7
  • 30
  • 53
-1

I will recommend singleton, but you can try something like that:

$GLOBAL = null;

And every time you would like to initate it:

if ($GLOBAL != null()) {
    $GLOBAL = new YourObject();
}
MacEncrypted
  • 184
  • 9
-2

Simply instantiate your object in the global scope. An example follows below:

#file1.php
require_once('file2.php');
$instance = new ClassName();

#file2.php
function do_something()
{
    global $instance;
    #do stuff with with
}
halfer
  • 18,701
  • 13
  • 79
  • 158
Emmanuel Okeke
  • 1,342
  • 13
  • 16
  • 2
    wow just wow. `Global`s are never a solution. Especially not when the question is tagged OOP – PeeHaa Aug 19 '14 at 10:33
  • @PeeHaa if you read his question, you'd realise he was searching for a way to use a single object instance, without making the class a `Singleton`. Globals are the cleanest way to do that, so long as the developer stays aware of what he's declared. So you know, the question is tagged `OOP` because he's interested in `Classes` and `Objects`. – Emmanuel Okeke Aug 22 '14 at 09:18
  • 1
    http://triggerplug.com/2013/11/30/hahaha-no-meme-of-the-day/. On a more serious note: [Use global variables in a class](http://stackoverflow.com/a/11923384/508666) – PeeHaa Aug 22 '14 at 09:30