有没有办法检查一个类中是否存在函数? [英] Is there a way to check if a function exists within a class?

查看:105
本文介绍了有没有办法检查一个类中是否存在函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我传递一些post数据来执行一个基于post数据的函数,以确定这是否应该执行我试图使用以下内容:

  $ SP = new StoredProcedure(); 
$ b $ if(function_exists($ SP-> $ _ POST ['function']))
{
$ SP-> $ _ POST ['function']();
}
else
{
echo'函数不存在。';
}

不幸的是,这会传递以下错误:


注意:未定义的属性:StoredProcedure :: $ getFormList in
C:\DWASFiles\Sites\junglegym\VirtualDirectory0\site\wwwroot\ wp-content \plugins\qcore\qcore_waitress.php
on line 353 function does not exist。


I确定这个函数确实存在,而当我执行它时,没有 function_exists()



有没有办法检查一个函数是否存在于类中?

解决方案

method_exists 检查给定对象的类的方法:

文档链接:
http://www.php.net/method_exists

  if(method_exists( $ SP,$ _POST ['function'])){
{
$ SP-> $ _ POST ['功能']();
}
else
{
echo'函数不存在。';

function_exists() method_exists()用于这些检查。首先是常规函数,其次是 OOP 函数。


I'm passing some post data to execute a function based on post data, to determine if this should execute I've tried to use the following:

$SP = new StoredProcedure();

if(function_exists($SP->$_POST['function']))
{
    $SP->$_POST['function']();
}
else
{
    echo 'function does not exist.';
}

Unfortunately this passes the following error:

Notice: Undefined property: StoredProcedure::$getFormList in C:\DWASFiles\Sites\junglegym\VirtualDirectory0\site\wwwroot\wp-content\plugins\qcore\qcore_waitress.php on line 353 function does not exist.

I'm certain this function does exist, and when I execute it without the function_exists()

Is there a way to check if a function exists when it's inside a class?

解决方案

method_exists checks for method of a class for a given object:

Docs Link: http://www.php.net/method_exists

if(method_exists($SP, $_POST['function'])) {
    {
        $SP->$_POST['function']();
    }
    else
    {
        echo 'function does not exist.';
    }

function_exists() and method_exists() are for these checks. First is for regular functions and second for OOP functions.

这篇关于有没有办法检查一个类中是否存在函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆