gpt4 book ai didi

php - 是否有可能 "escape"PHP 中的方法名称,以便能够拥有与保留关键字冲突的方法名称?

转载 作者:可可西里 更新时间:2023-10-31 22:08:02 24 4
gpt4 key购买 nike

我在 PHP 中做 MVC,我想在我的 Controller 中有一个 list() 方法,以具有 URL/entity/list/parent_id,以显示属于该父级的所有“x” .

但是,我不能使用名为 list() 的方法,因为它是 PHP 保留关键字。

例如,在 VB.Net 中,如果我需要一个名称与保留关键字冲突的名称,我可以将其包装在 [reserved_name] 中。
在 SQL 中,您可以做同样的事情。
在 MySQL 中,您使用反引号 `

PHP 中是否有一些语法指定“将其视为标识符,而不是关键字”?

(注意:我知道我可以在没有 list() 方法的情况下使用路由来执行此操作。我也可以简单地调用其他操作。问题是 PHP 是否提供这种转义)

最佳答案

您可以使用__call() 方法来调用实现您的功能的私有(private)或公共(public)_list() 方法。

/**
* This line for code assistance
* @method array list() list($par1, $par2) Returns list of something.
*/
class Foo
{
public function __call($name, $args)
{
if ($name == 'list') {
return call_user_func_array(array($this, '_list'), $args);
}
throw new Exception('Unknown method ' . $name . ' in class ' . get_class($this));
}

private function _list($par1, $par2, ...)
{
//your implementation here
return array();
}
}

关于php - 是否有可能 "escape"PHP 中的方法名称,以便能够拥有与保留关键字冲突的方法名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2302771/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com