PHP call_user_func_array - 获取函数内的整个数组

发布于 2024-10-21 23:41:04 字数 299 浏览 1 评论 0原文

我正在尝试创建一个具有 SEO 友好 URL 的内容管理系统。我的问题是,通过 htaccess,我使我的 URL 通过 $_GET['url'] 传递。

为了获取“call_user_func_array”的参数,我只是通过 / 爆炸 $_GET['url']

我允许我的用户为 SEO 友好的 URL 创建短标签 < em>WITH 斜杠 (/),因此它们的短标签有时会作为更多参数传递。有没有办法在调用类/函数中再次收集所有参数?

I am trying to create a content management system with SEO friendly URLs .. My problem is, that with htaccess I have made my URLs passed through $_GET['url'].

To get the arguments for "call_user_func_array" I am just exploding $_GET['url'] by /

I am allowing my users to create shorttag to the SEO-friendly URLS WITH slash (/) and therefore their shorttag will sometimes be passed as more arguments. Is there a way to collect all the arguments again inside the call class/function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丶情人眼里出诗心の 2024-10-28 23:41:04
$args = func_get_args();

将为您提供传递给函数的所有参数的数组。这实际上是在撤销您使用 call_user_func_array() 所做的事情;

$args = func_get_args();

will give you an array of all arguments passed into the function. That's actually undoing what you've done by using call_user_func_array();

画尸师 2024-10-28 23:41:04

选项 1(将 array(array) 传递给 call_user_func_array):

$urls = explode('/', $_GET['url']);

// note that $urls already was an array
call_user_func_array(array('ClassName', 'MethodName'), array($urls));

// inside class ClassName    
function MethodName($urls) { // $urls is passed as array

}

选项 2(使用 func_get_args):

$urls = explode('/', $_GET['url']);

// pass the array itself as argument, each split is new function argument
call_user_func_array(array('ClassName', 'MethodName'), $urls);

// inside class ClassName
function MethodName() { // this function has many arguments

    $urls = func_get_args(); // gets all arguments into array
}

选项 3(使用 call_user_func) > 而不是 call_user_func_array):

$urls = explode('/', $_GET['url']);

// pass the array itself as argument
call_user_func(array('ClassName', 'MethodName'), $urls);

// inside class ClassName
function MethodName($urls) { // call_user_func will pass any argument on directly

}

也许选项 3 最适合您。然而,当您想要向函数添加更多参数时,可扩展性会受到限制,但这对于选项 2 来说也是如此。

Option 1 (passing array(array) to call_user_func_array):

$urls = explode('/', $_GET['url']);

// note that $urls already was an array
call_user_func_array(array('ClassName', 'MethodName'), array($urls));

// inside class ClassName    
function MethodName($urls) { // $urls is passed as array

}

Option 2 (using func_get_args):

$urls = explode('/', $_GET['url']);

// pass the array itself as argument, each split is new function argument
call_user_func_array(array('ClassName', 'MethodName'), $urls);

// inside class ClassName
function MethodName() { // this function has many arguments

    $urls = func_get_args(); // gets all arguments into array
}

Option 3 (using call_user_func instead of call_user_func_array):

$urls = explode('/', $_GET['url']);

// pass the array itself as argument
call_user_func(array('ClassName', 'MethodName'), $urls);

// inside class ClassName
function MethodName($urls) { // call_user_func will pass any argument on directly

}

Perhaps option 3 is the most appropriate for you. However you are limited in extensability when you want to add more arguments to function, but this is also true for option 2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文