Jump to content
Larry Ullman's Book Forums

Recommended Posts

When I user the instanceof keyword for the Singleton class in Ch07 page 218, it is returning true. See below.

// Create a second object to confirm:
$TEST = Config::getInstance();

// see what instanceof thinks...
if ($TEST instanceof Config) {
    echo '<pre>$TEST IS instance of Config</p>';
}
else
{
    echo '<pre>$TEST IS NOT instance of Config</p>';
}

It is echoing that $TEST IS instance of Config. Am I doing something wrong?

Link to comment
Share on other sites

I was curious what instanceof would return here. It's strange because I can't do a print_r() or a var_dump() of $TEST. Also, I thought the whole point of the example was that in the "Singleton pattern is a creational pattern pattern that will restrict an application to creating only a single instance of a particular class type." (p216) I'm just trying to make sense of the models and see if there are cases where the php's behavior is counter-intuitive.

Link to comment
Share on other sites

The Singleton should be returning the SAME instance when you go to create the second. But that should still be a Config instance. It doesn't error out, it just doesn't create a NEW instance. 

Link to comment
Share on other sites

 Share

×
×
  • Create New...