JohnD Posted July 1, 2014 Share Posted July 1, 2014 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 More sharing options...
Antonio Conte Posted July 2, 2014 Share Posted July 2, 2014 How do you think instanceof should work? You are checking if $TEST is an instance of Config - which it is. You might be thinking wrong here. Link to comment Share on other sites More sharing options...
JohnD Posted July 2, 2014 Author Share Posted July 2, 2014 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 More sharing options...
Larry Posted July 3, 2014 Share Posted July 3, 2014 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 More sharing options...
JohnD Posted July 3, 2014 Author Share Posted July 3, 2014 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. Link to comment Share on other sites More sharing options...
Recommended Posts