Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to include file in PHP?

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()
    {
    }
}
}
like image 483
Jean-Philippe Leclerc Avatar asked Mar 23 '11 01:03

Jean-Philippe Leclerc


People also ask

Where should I put my PHP files?

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.

What is allowed to be placed in a PHP include file?

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.


1 Answers

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.

like image 187
nana Avatar answered Oct 29 '22 23:10

nana