Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用带有参数的函数_exists()的PHP_Php - Fatal编程技术网

使用带有参数的函数_exists()的PHP

使用带有参数的函数_exists()的PHP,php,Php,我喜欢用PHP做实验。我制作了我自己的缩写MVC,我玩得很开心。这一切工作(分离显示和逻辑),并允许我使主题引导。使用亲吻方法。所以请不要回答MVC/OOP问题。只是一个私人项目 在下面的代码中,您可以看到我能够路由所有函数。然而,我只是注意到一件奇怪的事情,我不确定。我的大多数页面都很小,由于简单,很少有参数。所以我制作了一个更大的应用程序(游戏),当然需要参数,这就是我得到bug的原因:-) 当使用这种调用函数的方法时,它工作得非常完美,直到我发送了一个参数。然后它给了我 参数太少,无法验证

我喜欢用PHP做实验。我制作了我自己的缩写MVC,我玩得很开心。这一切工作(分离显示和逻辑),并允许我使主题引导。使用亲吻方法。所以请不要回答MVC/OOP问题。只是一个私人项目

在下面的代码中,您可以看到我能够路由所有函数。然而,我只是注意到一件奇怪的事情,我不确定。我的大多数页面都很小,由于简单,很少有参数。所以我制作了一个更大的应用程序(游戏),当然需要参数,这就是我得到bug的原因:-)

当使用这种调用函数的方法时,它工作得非常完美,直到我发送了一个参数。然后它给了我

参数太少,无法验证函数(),已传递0

但事实并非如此

所以,我理解为什么这不起作用,因为$function是一个变量而不是一个实际的函数,所以它类似于说$variable($argument),这是非法的

所以只要没有争论,它就行

查看代码,您可以看到我正在尝试完成什么(如果可能的话),如何完成?如果有的话。 编辑以澄清

 $view = 'default';
    if (isset($url[0])) $view         = $url[0];`code`
    if (isset($url[1])) $function     = $url[1];
    if (isset($url[2])) $arg         = $url[2];
    if (isset($url[3])) $arg2        = $url[3];    // used for 2 arguments`


if(!empty($function) && function_exists($function)){
        switch(true){
            // add your case logic for your hight arg count down to here
            case(!empty($arg) && !empty($arg2)) : $function($arg,$arg2);  break; // BOTH arg not empty
            case(!empty($arg) && empty($arg2))  : $function($arg);        break; // FIRST arg only
            case(empty($arg) && empty($arg2))   : $function();            break; // BOTH arg EMPTY
            case(empty($arg) &&
                 empty($arg2) &&
                 isset($_POST))                  :$function("hello world");       break; // BOTH arg EMPTY POST set
        }
    }
没有jquery,我更喜欢纯js 同样,这只是一个个人项目,探索PHP的一些特性

你们专业人士能帮我玩得开心吗? 谢谢

特别感谢@Imaginaroom为我指明了正确的方向:

以下是更新的代码:

 if(isset($function) && function_exists($function)){
        if(isset($_POST)) {
            call_user_func($function, $_POST);
        }else{
            call_user_func_array($function, array($arg, $arg2));
        }
再次感谢你们其他人。在我纠正这一点之后,似乎很多人都有类似的想法。伟大的头脑


虽然它需要一些清理。这一点现在应该是可行的。而且,更重要的是,它允许我删除大量冗余代码。

调用变量函数是我的工作。尽管有PHP/7的增强功能,它还是一个非常古老的特性。您可以使用:

  • 是参数计数,如果是固定的,并且变量中有它们
  • 在任何其他情况下

另外(不确定您是否需要它),您可以使用获取函数接收到的所有参数。

调用变量函数是您的工作。尽管有PHP/7的增强功能,它还是一个非常古老的特性。您可以使用:

  • 是参数计数,如果是固定的,并且变量中有它们
  • 在任何其他情况下

此外(不确定您是否需要它),您可以使用获取函数接收到的所有参数。

将函数移动到类中。然后,您可以简化代码:

class myClass {
    public function foo($arg = null, $arg2 = null) {}

    public function test($function, $arg, $arg2) {
        $args = array_filter([$arg, $arg2]);

        if (method_exists([$this, $function])) {
            if (empty($args) && !empty($_POST)) {
                $function('Hello world');
            } else {
                call_user_func_array([$this, $function], array_filter($args));
            }
        }
    }
}

将函数移到类中。然后,您可以简化代码:

class myClass {
    public function foo($arg = null, $arg2 = null) {}

    public function test($function, $arg, $arg2) {
        $args = array_filter([$arg, $arg2]);

        if (method_exists([$this, $function])) {
            if (empty($args) && !empty($_POST)) {
                $function('Hello world');
            } else {
                call_user_func_array([$this, $function], array_filter($args));
            }
        }
    }
}

因此,在我看来,
empty($function)
如果试图调用分配给变量
$function
的可调用函数,则可能会出现问题,这就是它失败的原因(尚未测试它)

但是,函数_exists只接受字符串作为参数,它应该表示一个函数名来检查它是否存在(显然)。因此,如果变量
$function
是函数的名称,则不能像
$function($arg,$arg2)
那样使用它,而是需要查看函数。 另一方面,如果将函数本身分配给变量
$function
,则需要从

if(!empty($function) && function_exists($function)) 


因此,在我看来,
empty($function)
如果试图调用分配给变量
$function
的可调用函数,则可能会出现问题,这就是它失败的原因(尚未测试它)

但是,函数_exists只接受字符串作为参数,它应该表示一个函数名来检查它是否存在(显然)。因此,如果变量
$function
是函数的名称,则不能像
$function($arg,$arg2)
那样使用它,而是需要查看函数。 另一方面,如果将函数本身分配给变量
$function
,则需要从

if(!empty($function) && function_exists($function)) 

从错误消息“函数validate(),0传递的参数太少”中,似乎
$function=='validate'
$arg
$arg2
为空。请注意,PHP函数还将数字
0
和字符串
“0”
视为空值

您应该使用检查变量是否为空字符串。如果使用从请求中获得的值设置
$arg
$arg2
,则它们是字符串(也可以是字符串数组)

您的代码可能需要重写:

if (is_callable($f)) {
    // Collect all the non-empty arguments in $arguments
    $arguments = [];
    // $arg[0] is the first character of the string stored in $arg
    // when it is set (it exists) the string is not empty
    if (isset($arg[0]) {
        $arguments[] = $arg;
    }
    // More arguments
    if (isset($arg2[0]) {
        $arguments[] = $arg2;
    }

    // Call the function with arguments
    call_user_func_array($f, $arguments);
}
阅读更多关于和的信息。

从错误消息“函数验证()的参数太少,0已通过”中,似乎
$function=='validate'
$arg
$arg2
为空。请注意,PHP函数还将数字
0
和字符串
“0”
视为空值

您应该使用检查变量是否为空字符串。如果使用从请求中获得的值设置
$arg
$arg2
,则它们是字符串(也可以是字符串数组)

您的代码可能需要重写:

if (is_callable($f)) {
    // Collect all the non-empty arguments in $arguments
    $arguments = [];
    // $arg[0] is the first character of the string stored in $arg
    // when it is set (it exists) the string is not empty
    if (isset($arg[0]) {
        $arguments[] = $arg;
    }
    // More arguments
    if (isset($arg2[0]) {
        $arguments[] = $arg2;
    }

    // Call the function with arguments
    call_user_func_array($f, $arguments);
}

阅读更多关于和的信息。

您可以将闭包与参数解包一起使用

$closureA = function ($a, $b) { return $a + $b; };
$closureB = function ($a) { return $a * 2; };
$closureC = function () { return 2; };

function run(Closure $closure, array $args = []) {
   return $closure(...$args);
}

echo run($closureA, [1,2]);
echo run($closureB, [2]);
echo run($closureC);
输出:

342

这是使用不同参数运行3个闭包的结果,即使其中一个没有参数

可以将闭包设置为以key作为函数名的数组,然后使用array_key_exists轻松检查是否存在此类闭包,如果存在,则使用run()函数调用它

示例如下:

$closures = [];
$closures['add'] = function ($a, $b) { return $a + $b; };
$closures['multiplyby2'] = function ($a) { return $a * 2; };
$closures['return2'] = function () { return 2; };

function run(string $name, array $args = []){
   global $closures;
   return array_key_exists($name, $closures) ? $closures[$name](...$args) : "no closure for given name";
}

echo run('add', [1,2]);
echo run('multiplyby2', [2]);
echo run('return2');
echo run('blabla');

可以将闭包与参数解包一起使用

$closureA = function ($a, $b) { return $a + $b; };
$closureB = function ($a) { return $a * 2; };
$closureC = function () { return 2; };

function run(Closure $closure, array $args = []) {
   return $closure(...$args);
}

echo run($closureA, [1,2]);
echo run($closureB, [2]);
echo run($closureC);
输出:

342

这是使用不同参数运行3个闭包的结果,即使其中一个没有参数

可以将闭包设置为以key作为函数名的数组,然后使用array_key_exists轻松检查是否存在此类闭包,如果存在,则使用run()函数调用它

示例如下:

$closures = [];
$closures['add'] = function ($a, $b) { return $a + $b; };
$closures['multiplyby2'] = function ($a) { return $a * 2; };
$closures['return2'] = function () { return 2; };

function run(string $name, array $args = []){
   global $closures;
   return array_key_exists($name, $closures) ? $closures[$name](...$args) : "no closure for given name";
}

echo run('add', [1,2]);
echo run('multiplyby2', [2]);
echo run('return2');
echo run('blabla');

实现这一点的方法有很多

function test($arg1=FALSE,$arg2=FALSE){ // setting default value

    // If you want to check if the values are numeric or something like that
    // $arg1 = $arg1?is_numeric($arg1)?$arg1:FALSE:FALSE;
    // $arg2 = $arg2?is_numeric($arg2)?$arg2:FALSE:FALSE;
    $bothValid = ($arg1 && $arg2)?TRUE:FALSE;

    // Then you can proceed the way you like
    if($bothValid){
        // code that uses both params
        // return something;
    }

    if($arg1){
        // code that uses argument 1
        // return something;
    }

    if($arg2){
        // code that uses argument 2
        // return something;
    }   
}
或类似的,如下所示

function test($type,$arg1=FALSE,$arg2=FALSE){ // setting default value

    // If you want to check if the values are numeric or something like that
    // $arg1 = $arg1?is_numeric($arg1)?$arg1:FALSE:FALSE;
    // $arg2 = $arg2?is_numeric($arg2)?$arg2:FALSE:FALSE;

    $bothValid = ($arg1 && $arg2)?TRUE:FALSE;
    switch ($type) {
        case ($type == 'add' && $bothValid):
            return $arg1 + $arg2;
        break;

        default:
            return 'Your error message';
        break;
    }
}
您可以按照func进行检查