पर कॉल करने से पहले कक्षा में फ़ंक्शन मौजूद है या नहीं, निम्न कोड में मैं call_user_func()
के साथ कक्षा को कॉल करता हूं।कॉल करें कि कॉल_user_func()
if(file_exists('controller/' . $this->controller . '.controller.php')) {
require('controller/' . $this->controller . '.controller.php');
call_user_func(array($this->controller, $this->view));
} else {
echo 'error: controller not exists <br/>'. 'controller/' . $this->controller . '.controller.php';
}
आइए कहें कि नियंत्रक के पास निम्न कोड है।
class test {
static function test_function() {
echo 'test';
}
}
जब मैं call_user_func('test', 'test_function')
पर कॉल करता हूं तो कोई समस्या नहीं है। लेकिन जब मैं एक ऐसा फ़ंक्शन कॉल करता हूं जो अस्तित्व में नहीं है तो यह काम नहीं करता है। अब मैं पहले जांचना चाहता हूं कि फ़ेस call_user_func
पर कॉल करने से पहले, टी क्लास टेस्ट में फ़ंक्शन मौजूद है या नहीं।
क्या कोई ऐसा फ़ंक्शन है जो जांचता है कि कक्षा में कोई फ़ंक्शन मौजूद है या कोई अन्य तरीका है कि मैं इसे कैसे देख सकता हूं?
if (method_exists('ClassName','method_name'))
call_user_func(etc...);
या भी:
if (method_exists($class_instance,'method_name'))
call_user_func(etc...);
[method_exists()] (http://php.net /manual/en/function.method-exists.php) –
धन्यवाद @ मार्कबकर! –
प्रत्येक मौजूदा विधि कॉल करने योग्य नहीं है -> [is_callable] (http://php.net/manual/en/function.is-callable.php) – a4c8b