spackt Posted September 10, 2020 Share Posted September 10, 2020 So i have a magic method getSetting() which may not be neccessary available i tried the following and it doesnt seem to work if (!method_exists($settings, 'getSetting')) { // throw error } Quote Link to comment https://forums.phpfreaks.com/topic/311458-method_exist/ Share on other sites More sharing options...
gw1500se Posted September 10, 2020 Share Posted September 10, 2020 What does "doesn't seem to work" mean? Quote Link to comment https://forums.phpfreaks.com/topic/311458-method_exist/#findComment-1581273 Share on other sites More sharing options...
maxxd Posted September 11, 2020 Share Posted September 11, 2020 11 hours ago, spackt said: which may not be neccessary available Not to hop on gw1500se's bandwagon, but explain this as well, please... Quote Link to comment https://forums.phpfreaks.com/topic/311458-method_exist/#findComment-1581287 Share on other sites More sharing options...
kicken Posted September 11, 2020 Share Posted September 11, 2020 I am guessing by magic method you mean you are using __call to respond to calls for getSettings(). If that's the case, then you cannot use method_exists to check if that "method" is defined because PHP has no way of actually checking what __call handles and what it doesn't. You'd have to provide your own means of determining if that "method" is defined. One option is to just call it and have __call throw an exception if it's not handled. You can then catch that exception and handle it however you want. I think a better solution would be to abandon __call entirely and design your code in a way that getSettings is a real method. Perhaps you could define it as part of an interface then check whether or not your object implements that interface. Quote Link to comment https://forums.phpfreaks.com/topic/311458-method_exist/#findComment-1581289 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.