Blog

Comments on: method_exists() vs. is_callable() http://blog.jmfeurprier.com/2010/01/03/method_exists-vs-is_callable/ LAMP Web Developer Wed, 16 Mar 2011 13:53:48 +0000 http://wordpress.org/?v=2.9.1 hourly 1 By: dbind http://blog.jmfeurprier.com/2010/01/03/method_exists-vs-is_callable/comment-page-1/#comment-2980 dbind Wed, 16 Mar 2011 13:53:48 +0000 http://blog.jmfeurprier.com/2010/01/03/method_exists-vs-is_callable/#comment-2980 @mdomba, it depends on what you're attempting. 1. If you just want to make sure you can call it, use is_callable. If you need to make sure that the method exists by that name (meaning it's not callable just through a magic method) then use both. 2. If you just want to know if the method exists, not caring whether you can call it at that point, then method_exists alone is enough. 3. If you want to call the method and you don't care why or how it's callable, but just that it is, then use is_callable alone. Just notice the purpose of each: is_callable tells you whether calling it will abort the script with an error, while method_exists tells you if that method was actually hard-coded in a particular class, not caring if it's gonna explode in your face when you try to call it. @mdomba, it depends on what you’re attempting.

1. If you just want to make sure you can call it, use is_callable. If you need to make sure that the method exists by that name (meaning it’s not callable just through a magic method) then use both.

2. If you just want to know if the method exists, not caring whether you can call it at that point, then method_exists alone is enough.

3. If you want to call the method and you don’t care why or how it’s callable, but just that it is, then use is_callable alone.

Just notice the purpose of each: is_callable tells you whether calling it will abort the script with an error, while method_exists tells you if that method was actually hard-coded in a particular class, not caring if it’s gonna explode in your face when you try to call it.

]]>
By: mdomba http://blog.jmfeurprier.com/2010/01/03/method_exists-vs-is_callable/comment-page-1/#comment-2482 mdomba Wed, 16 Feb 2011 09:49:06 +0000 http://blog.jmfeurprier.com/2010/01/03/method_exists-vs-is_callable/#comment-2482 So to be sure for all situations... the best usage would be... if method_exist(...) && is_callable(...) So to be sure for all situations… the best usage would be…

if method_exist(…) && is_callable(…)

]]>
By: abcphp.com http://blog.jmfeurprier.com/2010/01/03/method_exists-vs-is_callable/comment-page-1/#comment-17 abcphp.com Sun, 03 Jan 2010 10:49:02 +0000 http://blog.jmfeurprier.com/2010/01/03/method_exists-vs-is_callable/#comment-17 <strong>method_exists() vs. is_callable()...</strong> One thing I often see when re-factoring PHP applications, is the improper use of the method_exists() function, and I think this needs a little bit of clarification.... method_exists() vs. is_callable()…

One thing I often see when re-factoring PHP applications, is the improper use of the method_exists() function, and I think this needs a little bit of clarification….

]]>
By: method_exists() vs. is_callable() | Coder Online http://blog.jmfeurprier.com/2010/01/03/method_exists-vs-is_callable/comment-page-1/#comment-15 method_exists() vs. is_callable() | Coder Online Sun, 03 Jan 2010 08:30:06 +0000 http://blog.jmfeurprier.com/2010/01/03/method_exists-vs-is_callable/#comment-15 [...] Read more: method_exists() vs. is_callable() [...] [...] Read more: method_exists() vs. is_callable() [...]

]]>