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?

Share this post


Link to post
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.

Share this post


Link to post
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. 

Share this post


Link to post
Share on other sites

Yes, I see now. I think was confused by the object variable $CONFIG having the same name as the class. Thank you both for the help.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...