如何弃用 PHP 中的函数?

发布于 2024-07-07 20:49:11 字数 250 浏览 9 评论 0原文

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

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

最好,我仍然希望它们可用,但使其在使用时抛出“通知”或“警告”。

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

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.

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

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.

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

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

发布评论

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

评论(6

弱骨蛰伏 2024-07-14 20:49:11

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

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

    return $this;
}

@deprecated phpdoc 很重要,因为许多 IDE(例如 PHPStorm)都能识别它并进行攻击方法名称(如果您尝试使用它),因此您会注意到在实际运行代码之前它已被弃用。

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

jetbrains deprecated Strikethrough

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

E_DEPRECATED 由 PHP 在内部使用,因此您不应该使用它。 更多信息请参见此处

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_error along with some phpdoc.

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

    return $this;
}

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

It will look more or less like this:

jetbrains deprecated strikethrough

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).

E_DEPRECATED is instead used internally by PHP thus you should not be using it. More information here.

牵强ㄟ 2024-07-14 20:49:11

trigger_error()

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

trigger_error()

function my_deprecated_function() {
    trigger_error("Deprecated function called.", E_USER_NOTICE);
    // do stuff.
}
巷雨优美回忆 2024-07-14 20:49:11

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

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 Function function.

沙沙粒小 2024-07-14 20:49:11

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

编辑:好吧,这还不起作用 - 所以不要使用 E_USER_DEPRECATED,只需使用类似 E_USER_NOTICE 的东西:

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

$foo = new Foo()

这将最终得到以下结果:

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

I haven't checked it by myself, but found this in my bookmarks: 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:

<?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
复古式 2024-07-14 20:49:11

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

或者,如果函数位于单个文件中,则每当该文件包含在其他位置时,在文件顶部触发弃用警告就会显示错误。

最后,您可以在任何已弃用函数的第一行抛出错误。

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

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.

别低头,皇冠会掉 2024-07-14 20:49:11

您可以考虑编写一个脚本,该脚本可以扫描您的代码库以查找此函数的使用情况,然后生成违规代码的报告,而不是引发有关使用的运行时警告。 偶尔运行一下。

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

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.

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