php - 将变量从数组解析到方法

标签 php oop url-routing

我正在尝试创建自己的小型 MVC 系统,它工作得很好,但我遇到的一件事是将变量解析为方法。

你看我使用URL重写来使每个URL都指向index.php,然后通过像/email/1/34/这样的URL数据来设置页面

然后我将创建一个像这里这样的对象。

<?php 
$page = $urlsplit[0];

$variables = array($urlsplit[1], $urlsplit[2]);
$page->callmethod($variables);
?>

我想要它做的是,它应该这样做,而不是将数组解析为方法。

$page->callmethod($variables[0], $variables[1]);

知道我该怎么做吗?

最佳答案

要动态调用 $page->callmethod($variables[0], $variables[1]) ,您可以使用 call_user_func_array :

call_user_func_array(array($page, 'callmethod'), $variables);

关于php - 将变量从数组解析到方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9959447/

相关文章:

asp.net - MVC 3 中的 URL 路由

url - .url 文件不加载图标

php - Apache/Httpd 与 PHP 5.5 一起使用的 httpd_module 名称是什么?

php - 数据库最常见条目查询

c# - 方法的返回值正在修改作为方法参数传递的对象

oop - 实体是否应该有行为?

javascript - React-router:URL参数存在于this.props中的两个不同位置?

php - 使用 PHPThumb 和 Thickbox/Colorbox

PHP 到 Excel : fill the output Excel file with a loop

arrays - 从类数组创建实例