【问题标题】:Defining good method of having an alias function定义具有别名函数的好方法
【发布时间】:2012-08-18 01:09:11
【问题描述】:

由于向后兼容性问题,我需要两个函数,一个是另一个的别名。

public function getMostPopularArticles(array $params = array())
{
    $this->getMostViewedArticles($params);
}

public function getMostViewedArticles(array $params = array())
{
    return $this->_response_request(".....");
}

getMostPopularArticles() 是新函数,它是旧函数的别名(及时将被删除)getMostViewedArticles()

我的做法对吗?有改进的余地吗?

我不得不提到getMostViewedArticles()(旧函数)正在使用新参数来返回正确的数据,但我只需要一个具有正确名称的别名(getMostPopularArticles()),以便用户有时间更新他们的代码,并在未来的版本中完全删除旧的命名函数。

【问题讨论】:

  • PHP 什么时候让你为内置类型输入提示?
  • Typehints 适用于至少 PHP 5.3 的数组。

标签: php function optimization


【解决方案1】:

你为什么不这样做:

public function getMostViewedArticles(array $params = array())
{
    return getMostPopularArticles($params);
}

这样,任何新的更改只需要在getMostPopularArticles 中完成,但getMostViewedArticles 仍然可以被旧代码使用。

另外getMostPopularArticles 应该有你当前使用的任何业务逻辑,因为这是你想要继续使用的新功能

public function getMostPopularArticles(array $params = array())
{
    return $this->_response_request(".....");
}

【讨论】:

    【解决方案2】:

    如果你有一个不推荐使用的函数,你应该在所有新代码中基本上避开它。删除该函数后,您的代码将(显然)中断。因此,您不应调用已弃用的函数:而应在新调用中复制并粘贴(或重写)代码。

    【讨论】:

    • 感谢您的回答,对于没有完全解释所有内容,我深表歉意,因此,请查看我的问题更新
    猜你喜欢
    • 2014-06-14
    • 1970-01-01
    • 2015-11-25
    • 1970-01-01
    • 1970-01-01
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 2012-12-15
    相关资源
    最近更新 更多