如何弃用 PHP 中的函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/194121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:53:42  来源:igfitidea点击:

How to deprecate a function in PHP?

phpdeprecated

提问by Milan Babu?kov

At the team with which I work, we have an old codebase using PHP's ibase_* functions all over the code to communicate with database. We created a wrapper to it that would do something else beside just calling the original function and I did a mass search-replace in the entire code to make sure that wrapper is used instead.

在与我一起工作的团队中,我们有一个旧的代码库,在代码中使用 PHP 的 ibase_* 函数与数据库进行通信。我们为它创建了一个包装器,除了调用原始函数之外,它还可以做其他事情,我在整个代码中进行了大量搜索替换,以确保使用包装器。

Now, how do we prevent usage of ibase_* functions in the future?

现在,我们如何防止将来使用 ibase_* 函数?

Preferably, I'd like to still have them available, but make it throw a NOTICE or WARNING when it is used.

最好,我希望它们仍然可用,但在使用时让它发出通知或警告。

A solution in pure PHP (not needing to compile a custom version of PHP) is preferred.

首选纯 PHP 解决方案(不需要编译自定义版本的 PHP)。

采纳答案by Francesco Casula

If I understand correct, you want to trigger an error when a built-in PHP function is used? In that case, take a look at the Override Functionfunction.

如果我理解正确,您想在使用内置 PHP 函数时触发错误吗?在这种情况下,请查看Override Function函数。

回答by nickf

trigger_error()

触发错误()

function my_deprecated_function() {
    trigger_error("Deprecated function called.", E_USER_NOTICE);
    // do stuff.
}

回答by Francesco Casula

Generally speaking you can flag a method as deprecated to give your users warnings about code that will not work in future versions. I think the best way is to use trigger_erroralong with some phpdoc.

一般来说,您可以将某个方法标记为已弃用,以便向您的用户警告在未来版本中将无法使用的代码。我认为最好的方法是使用trigger_error和一些phpdoc

/**
 * @deprecated
 *
 * @return $this
 */
public function oldMethod()
{
    trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED);

    return $this;
}

The @deprecatedphpdoc is important because many IDEs like PHPStormrecognise it and strike the method name if you try to use it, so you notice that is deprecated before actually running your code.

@deprecated因为像许多IDE PHPDoc的重要PHPStorm承认它和罢工的方法名,如果你尝试使用它,所以你会注意到在实际运行您的代码之前弃用。

It will look more or less like this:

它或多或少看起来像这样:

jetbrains deprecated strikethrough

jetbrains 已弃用删除线

Beside the phpdoc you can make sure the user gets a warning by triggering the right error at runtime. Just make sure you use the right constant (i.e. E_USER_DEPRECATED).

除了 phpdoc,您还可以通过在运行时触发正确的错误来确保用户收到警告。只要确保使用正确的常量(即E_USER_DEPRECATED)。

E_DEPRECATEDis instead used internally by PHP thus you should notbe using it. More information here.

E_DEPRECATED相反由PHP内部使用因此,你应该使用它。更多信息在这里

回答by unexist

I haven't checked it by myself, but found this in my bookmarks: http://wiki.php.net/rfc/e-user-deprecated-warning

我没有自己检查过,但在我的书签中找到了这个:http: //wiki.php.net/rfc/e-user-deprecated-warning

Edit: Okay this doesn't work yet - so instead of E_USER_DEPRECATED just use something like E_USER_NOTICE:

编辑:好的,这还行不通 - 所以,而不是 E_USER_DEPRECATED 只是使用类似 E_USER_NOTICE 的东西:

<?php
class Foo
{   
    public function __construct()
    {
        trigger_error('Use Bar instead', E_USER_NOTICE);
    }
}

$foo = new Foo()

This will end up with this:

最终结果如下:

Notice: Use Bar instead in /home/unexist/projects/ingame/svn/foo.php on line 6

回答by Codebeef

If your functions are part of a class, then you could use trigger_errorin the constructor to warn of the deprecation.

如果您的函数是类的一部分,那么您可以在构造函数中使用trigger_error来警告弃用。

Alternatively, if the functions are in a single file, then triggering a deprecation warning at the top of the file would show the error whenever the file is included elsewhere.

或者,如果函数位于单个文件中,则只要文件包含在其他地方,则在文件顶部触发弃用警告将显示错误。

Finally, you could throw the error on the first line of any of the deprecated functions.

最后,您可以在任何不推荐使用的函数的第一行抛出错误。

回答by troelskn

Instead of raising a runtime warning on usage, you could consider writing a script, that can scan your code base for the use of this function, then generate a report of offending code. Once in a while, run it through.

您可以考虑编写一个脚本,而不是在使用时发出运行时警告,该脚本可以扫描您的代码库以使用此功能,然后生成违规代码的报告。偶尔,运行它。

If you use a version control system, you could set the script as a commit-hook. I would probably recommend a post-hook, that simply sends an email, when a script, containing deprecated functions, is checked in, but if you really want to enforce it, you could have a pre-hook completely prevent anyone from checking it in.

如果您使用版本控制系统,则可以将脚本设置为提交挂钩。我可能会推荐一个 post-hook,它只发送一封电子邮件,当包含不推荐使用的函数的脚本被签入时,但如果你真的想强制执行它,你可以有一个 pre-hook 完全阻止任何人签入它.