so let’s say you have a singleton pattern or whatever:
class Smth{
public static function Foo(){
static $instance;
if(!condition()) return false; // <-- it's nothing...
if(!($instance instanceof FooClass)) $instance = new FooClass();
return $instance; // <-- it's a object and has that method
}
}
so if I call Smth::foo()->A_foo_method() when condition() is met, then the method is executed and everything is OK.
But if condition() is not met, obviously I get a fatal error telling me that Smth::foo() is not a object etc…
How can I simply ignore the 2nd case.? I mean don’t do anything, and don’t show the fatal error.
(besides checking the condition() outside the class, when calling the method)
You do not want to do that. This is a silent failure and it’s not a good thing. When you call a method, you expect it to do something (especially a
getInstance-like method in the Singleton pattern, which should return an instance). So yes, you have to check iffoo()returns an actual object before callingA_foo_method(). Silently failing instead could create a debugging mess.