41

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.

6 Answers 6

50

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.

3
  • The problem with this is that at the error message gives the line of the call to trigger_error() not the line of the call to the deprecated function. However I don't know of a better way of achieving this, so I've still up-voted it. Sep 30, 2022 at 14:34
  • Yeah, that's good enough though given that the objective here is to be able to pin-point the deprecated function and this does it. Thanks for pointing it out though. Oct 3, 2022 at 16:02
  • You could add print_r(debug_backtrace(), 1) call to the trigger_error message if you want to know where it was called. Have a look here for prettier ways of printing the trace: stackoverflow.com/questions/4282120/…
    – Christiaan
    Nov 17, 2022 at 15:37
48

trigger_error()

function my_deprecated_function() {
    trigger_error("Deprecated function called.", E_USER_NOTICE);
    // do stuff.
}
2
  • 1
    It is not my function, but PHP's built in ibase_* functions like ibase_query for example. Oct 11, 2008 at 15:47
  • 18
    You may consider using E_USER_DEPRECATED for PHP 5.3.x and later versions
    – emont01
    Dec 5, 2014 at 20:28
16

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.

3
  • Thanks, following the link you posted, I found exactly what I needed, here: no.php.net/manual/en/function.override-function.php#50821 Oct 11, 2008 at 17:15
  • 1
    Keep in mind that this is part of the APD (debugger) extension. You should not use this in your production environment, and even on your test machine, it will probably conflict with other debuggers (Such as Xdebug), so you may not want to use that.
    – troelskn
    Oct 11, 2008 at 17:31
  • Not at runtime, I don't. I would suggest static analysis (preg should do for simple cases), on your test-environment.
    – troelskn
    Oct 19, 2008 at 12:25
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
3

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.

1
  • It is not my function, but PHP's built in ibase_* functions like ibase_query for example. Oct 11, 2008 at 15:47
2

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.