Reputation: 1978
while seeing word press core code in depth i came across a file which has many empty functions for eg :
/**
* @ignore
*/
function apply_filters() {}
i realy dont know what is the use of declaring empty function in php..
i found this in wp-admin/list-scripts.php on line 34 - 37
and in wp-include/plugin.php on line 163 - 207 the same function is re declared with some works in it
In total i have 2 questions
What is the use of declaring an empty function in php
Why wordpress din't show any Fatal error:
as the same function is already declared. ?
Upvotes: 1
Views: 147
Reputation: 2568
In PHP (and many other OOP languages), an empty function (or, more precisely, method) can be used in an interface. Any class inheriting that interface must implement the declared functions. However, last time I checked (which is, 2 minutes ago), WordPress isn't really an OOP system, so forward to 2.
list-scripts.php is not a default WordPress file - I can't find it in any of my WP installation. You may want to test by putting a die('called');
on top of the file and see if it gets executed. Therefore, WordPress won't encounter duplicated function declaration, and no fatal errors are introduced.
Now, even if list-scripts.php is a default WP file, when working with WP (and PHP in general) more often than not you see this:
if (!function_exists('apply_filters')) {
function apply_filters($arg1, $arg2) {
// code is poetry
}
}
This makes sure a function is only declared if it hasn't been before, and avoids the fatal error.
Upvotes: 1
Reputation: 158090
I guess wordpress will conditionally include either one or the other file. A lower level API of wordpress expects this functions to be defined and calls them. The extension itself is free to implement the function or not, however it has at least to provide the empty function body. The concepts behind this are much like interfaces work in OOP.
Upvotes: 1