Php 定义具有别名函数的好方法
由于向后兼容性问题,我需要两个函数,其中一个是另一个的别名Php 定义具有别名函数的好方法,php,function,optimization,Php,Function,Optimization,由于向后兼容性问题,我需要两个函数,其中一个是另一个的别名 public function getMostPopularArticles(array $params = array()) { $this->getMostViewedArticles($params); } public function getMostViewedArticles(array $params = array()) { return $this->_response_request("
public function getMostPopularArticles(array $params = array())
{
$this->getMostViewedArticles($params);
}
public function getMostViewedArticles(array $params = array())
{
return $this->_response_request(".....");
}
getMostPopularArticles()
是新函数,它是旧函数的别名(将被删除)getMostViewedArticles()
我这样做对吗?还有改进的余地吗
我必须提到的是,
getMostViewedArticles()
(旧函数)正在使用新参数返回正确的数据,但我只需要一个具有正确名称的别名(getMostPopularArticles()
),这样用户就有时间更新他们的代码,在未来的版本中,完全删除旧的命名函数。如果您有一个不推荐使用的函数,您应该在所有新代码中基本上避免使用它。当函数被删除时,您的代码将(显然)中断。因此,您不应该调用不推荐使用的函数:您应该在新调用中复制并粘贴代码(或重写代码)。为什么不这样做:
public function getMostViewedArticles(array $params = array())
{
return getMostPopularArticles($params);
}
这样,任何新的更改只需要在getMostPopularArticles
中完成,但是getMostViewedArticles
仍然可以被遗留代码使用
另外,getMostPopularArticles
应该具有您当前使用的任何业务逻辑,因为这是您希望继续使用的较新功能
public function getMostPopularArticles(array $params = array())
{
return $this->_response_request(".....");
}
PHP什么时候允许您使用内置类型的typehint?至少从PHP 5.3开始,typehint适用于数组。感谢您的回答,我很抱歉没有完全解释所有内容,因此,请查看我的问题更新