Autoloading in PHP (include,require) statement?
Autoloading
Because we are going to be writing a lot of code, it is safe to assume that we would want to structure the code clearly, to avoid confusion as the amount of code increases. There are a few ways in which we could do this, and one of them is keeping separate chunks of code in separate files. A problem that arises from this approach to code structure is that we will need to reference these external chunks of code from a single point of entry.Requests to a web server tend to be routed to a single file in the server’s webroot directory.
For example,Apache web servers are usually configured to route default requests to index.php. We can keep all our framework code in this single file, or we can split it into multiple files and reference them from within index.php. PHP provides four basic statements for us to include external scripts within the program flow.
The Four Basic require/include Functions
include("events.php");
include_once("inflection.php");
require("flash.php");
require_once("twitter.php");
Description of 4 statement in PHP.
1.The first statement will look for events.php within the PHP include path, and if it exists, PHP will load the file. If it cannot find the file, it will emit a warning.
2.The second statement is the same as the first, except that it will only ever attempt to load the file inflection.php once. You can run the second statement as many times as you want, but the file will only be loaded the first time.
3.The third statement is much the same as the first, except that PHP will emit a fatal error (if uncaught it will stop execution of the script).
4.The fourth statement is the same as the second, except that it will also emit a fatal error if the file cannot be found.
This means loading of classes is sufficient on a small scale, but it does have the following few drawbacks:
.You will always need to require/include the files that contain your scripts before you
can use them. This sounds easy at first, but in a large system it is actually quite a painful
process. You need to remember the path to each script and the right time to include them.
• If you opt to include all the scripts at the same time (usually at the top of each file), they
will be in scope for the entire time the script is executing. They will be loaded first, before
the script that requires them, and fully evaluated before anything else can happen.
This is fine on a small scale, but can quickly slow your page load times.
Because we are going to be writing a lot of code, it is safe to assume that we would want to structure the code clearly, to avoid confusion as the amount of code increases. There are a few ways in which we could do this, and one of them is keeping separate chunks of code in separate files. A problem that arises from this approach to code structure is that we will need to reference these external chunks of code from a single point of entry.Requests to a web server tend to be routed to a single file in the server’s webroot directory.
For example,Apache web servers are usually configured to route default requests to index.php. We can keep all our framework code in this single file, or we can split it into multiple files and reference them from within index.php. PHP provides four basic statements for us to include external scripts within the program flow.
The Four Basic require/include Functions
include("events.php");
include_once("inflection.php");
require("flash.php");
require_once("twitter.php");
Description of 4 statement in PHP.
1.The first statement will look for events.php within the PHP include path, and if it exists, PHP will load the file. If it cannot find the file, it will emit a warning.
2.The second statement is the same as the first, except that it will only ever attempt to load the file inflection.php once. You can run the second statement as many times as you want, but the file will only be loaded the first time.
3.The third statement is much the same as the first, except that PHP will emit a fatal error (if uncaught it will stop execution of the script).
4.The fourth statement is the same as the second, except that it will also emit a fatal error if the file cannot be found.
This means loading of classes is sufficient on a small scale, but it does have the following few drawbacks:
.You will always need to require/include the files that contain your scripts before you
can use them. This sounds easy at first, but in a large system it is actually quite a painful
process. You need to remember the path to each script and the right time to include them.
• If you opt to include all the scripts at the same time (usually at the top of each file), they
will be in scope for the entire time the script is executing. They will be loaded first, before
the script that requires them, and fully evaluated before anything else can happen.
This is fine on a small scale, but can quickly slow your page load times.
No comments