Reputation: 4097
Is it possible to control the flow of all include/require
statements in PHP; whether they're invoked by an autoloader or elsewhere?
I'm trying to intercept all inclusions and place my custom file before all others, with require_once
so it won't get used by an autoloader or something/someone else.
get_included_files/get_required_files
only list included files, but is there a workaround to place a file on top of that list, or just to control the flow of the inclusions, whether if there's an autoload or not?
Upvotes: 1
Views: 292
Reputation: 22770
I'm trying to intercept all inclusions and place my custom file before all others, with require_once so it won't get used by an autoloader or something/someone else.
While you mention "intercept" you don't seem to need to actually intercept more, you're wanting to push in front of them. Please clarify if this is a misunderstanding.
To place my custom file before all others
you can use auto_prepend_file in php.ini
(as stated by axiac).
you can do this in your account specific php.ini (usually called user.ini
or similar) which is read before any PHP content files:
; Wordfence WAF Example
auto_prepend_file = '/home/accountname/public_html/wordfence-waf.php'
; END Wordfence WAF
The above means that when any PHP file is loaded on this account, that the wordfence-waf.php
file is called and run first. This file has no idea what includes will run after it.
This is the closest PHP has to what I think you're trying to achieve. It may help if you clarify the wording of your question.
Upvotes: 2
Reputation: 72376
Is it possible to control the flow of all
include
/require
statements in PHP; whether they're invoked by an autoloader or elsewhere?
No, the PHP interpreter does not provide any way to hook into its mechanism of including files.
The only way to control what files are included is through an autoloader but it is invoked only when the code reaches a reference to an unknown class, interface or trait. It is not invoked when a file is include
d/require
d directly.
I'm trying to intercept all inclusions and place my custom file before all others, with
require_once
so it won't get used by an autoloader or something/someone else.
As I tried to explain in my comments, when you write an application (CLI application or website) you control most file inclusions. You can just include
/require
the desired file before anything else and that's all. It won't be the first file in the output of get_included_files()
because it is not the first file loaded and executed. The main application file (the one that include
s it) will always be the first one in list.
If you work on a library (i.e. a bunch of files that provide some useful functionality to be used in applications) then you cannot control the order of file inclusions. Several files of the application and of other libraries will be, most probably, already included when your code is included.
get_included_files
/get_required_files
only list included files, but is there a workaround to place a file on top of that list, or just to control the flow of the inclusions, whether if there's an autoload or not?
The first file in the list returned by get_included_files()
is always the main script; i.e. the one invoked by the web server or launched in execution using the CLI version of PHP. It includes the other files.
You can use the auto_prepend_file
php.ini
setting to configure your PHP interpreter (both the CGI and CLI versions) to load a certain PHP file before any main script it loads.
It is still not what you want, it doesn't provide any hook into the inclusion mechanism. All it does is to require
the provided file on top of every main script it runs (as if every PHP script you request from the server starts with an invisible require
statement).
Upvotes: 2
Reputation: 1466
There are no events so no listening.
Only way I could think of (apart from changing the source code of PHP in which case you should rethink the whole thing) is wrapping the include/require inside your custom function. Like this :
function custom_include($file){
require_once("my_file.php");
require_once($file);
}
Upvotes: 0