views:

3171

answers:

5

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.

+1  A: 

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
unexist
+6  A: 

trigger_error()

function my_deprecated_function() {
    trigger_error("Deprecated function called.", E_USER_NOTICE);
    // do stuff.
}
nickf
It is not my function, but PHP's built in ibase_* functions like ibase_query for example.
Milan Babuškov
+2  A: 

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.

Mr. Matt
It is not my function, but PHP's built in ibase_* functions like ibase_query for example.
Milan Babuškov
+10  A: 

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.

Thanks, following the link you posted, I found exactly what I needed, here:http://no.php.net/manual/en/function.override-function.php#50821
Milan Babuškov
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
@troelskn: you're right. Any other idea?
Milan Babuškov
Not at runtime, I don't. I would suggest static analysis (preg should do for simple cases), on your test-environment.
troelskn
+2  A: 

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.

troelskn
It's called GREP
Malfist