I'm currently developping a PHP web application and I would like to know what is the best manner to include files (include_once) in a way where the code it is still maintanable. By maintanable I mean that if I want to move a file, It'd be easy to refactor my application to make it work properly.
I have a lot of files since I try to have good OOP practices (one class = one file).
Here's a typical class structure for my application:
namespace Controls
{
use Drawing\Color;
include_once '/../Control.php';
class GridView extends Control
{
public $evenRowColor;
public $oddRowColor;
public function __construct()
{
}
public function draw()
{
}
protected function generateStyle()
{
}
private function drawColumns()
{
}
}
}
If your server supports PHP, then you do not need to do anything. Just create your . php files, put them in your web directory and the server will automatically parse them for you.
Including a PHP File into Another PHP File The include() and require() statement allow you to include the code contained in a PHP file within another PHP file. Including a file produces the same result as copying the script from the file specified and pasted into the location where it is called.
I used to start all my php file with:
include_once('init.php');
Then in that file I would require_once all the other files that needed to be required, like functions.php for example, or globals.php where I would declare all global variables, or constants. That way you only have to edit all your setting at one place.
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