Jump to content
Sign in to follow this  
spackt

!method_exist

Recommended Posts

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
}

 

Share this post


Link to post
Share on other sites
11 hours ago, spackt said:

which may not be neccessary available

Not to hop on gw1500se's bandwagon, but explain this as well, please...

Share this post


Link to post
Share on other sites

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.

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

Sign in to follow this  

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.