Calling php -l
checks the syntax of a php file.
Is there a way to check for undefined functions? I don't need it to work with functions defined at runtime nor with dynamic calls.
Calling the file obviously won't work: I need to check the whole file, not only the branch that gets executed. See the follwing example.
myfile.php:
function imhere () {}
main.php:
require_once 'myfile.php';
if (true) {
imhere();
}
else {
imnhothere(); // I need to get a warning about this
}
Determining if a PHP function is available You can determine whether or not a PHP function is enabled for your web site by using the function_exists() function.
Code Inspection: Undefined functionReports the references to functions that are not defined in the project files, configured include paths, or among the PHP predefined functions. In the following example, the undefined_function() is not defined in the built-in library and project files.
The solution simply is to check if it has been set before referencing. We can use the isset() function, which checks if the variable 'is set' or not before referencing to them. Isset() function returns True or False depending on it.
Checking for undefined functions is impossible in a dynamic language. Consider that undefined functions can manifest in lots of ways:
undefined();
array_filter('undefined', $array);
$prefix = 'un'; $f = $prefix.'defined'; $f();
This is in addition to the fact that there may be functions that are used and defined conditionally (through includes or otherwise). In the worst case, consider this scenario:
if(time() & 1) {
function foo() {}
}
foo();
Does the above program call an undefined function?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With