PHP Include Best Practices Question

PHP include best practices question

I include my views from my controllers. I also define file locations to make maintenance easier.

config.php

define('DIR_BASE',      dirname( dirname( __FILE__ ) ) . '/');
define('DIR_SYSTEM', DIR_BASE . 'system/');
define('DIR_VIEWS', DIR_SYSTEM . 'views/');
define('DIR_CTLS', DIR_SYSTEM . 'ctls/');
define('DIR_MDLS', DIR_SYSTEM . 'mdls/');
define('VIEW_HEADER', DIR_VIEWS . 'header.php');
define('VIEW_NAVIGATION', DIR_VIEWS . 'navigation.php');
define('VIEW_FOOTER', DIR_VIEWS . 'footer.php');

Now i have all the info i need just by including config.php.

controller.php

require( '../config.php' );
include( DIR_MDLS . 'model.php' );

$model = new model();
if ( $model->getStuff() ) {
$page_to_load = DIR_VIEWS . 'page.php';
}
else {
$page_to_load = DIR_VIEWS . 'otherpage.php';
}

include( VIEW_HEADER );
include( VIEW_NAVIGATION );
include( DIR_VIEWS . $page_to_load );
include( VIEW_FOOTER );

What is the best practice for including PHP files?

EDIT 2016

Well, 5 years since I replied this. I am still alive. A lot has changed.

Now I use autoloaders to include my files. Here is official info for autoloaders with examples.

Basically, the idea is to have a proper folder structure (PSR-4 standards for instance) and having a class within each file. This way you can use autoloaders and PHP will load your files automatically.

OLD ANSWER

Usually, I have a config file like this:

define(root, $_SERVER['DOCUMENT_ROOT']);
.... // other variables that are used a lot

include (root . '/class/database.php');
.... // other includes that are mostly called from each file, like a db class, or user class, functions etc etc...

if (defined('development'))
{
// turn error reporting on
}
else
{
// turn it off
}

etc etc... You got the point of config.

And I include the config.php on each file. I forgot how to do it right now, but apache can do the automatic include for you. Therefore, you can say to apache to include your config file by default.

Then, I have controller classes, which call the views. There in each function I call the view.

someController.php

function index() { include root . '/views/view_index.php'; }

finally, from the view, if I need to include the header and footer view I do it like this:

view_index.php

<?include root . '/view/shared/header.php';?>
<div class="bla bla bla">bla bla bla</div>
<?include root . '/view/shared/footer.php';?>

I always use include in this structure rather than include_once since the latter requires extra check. I mean, since I am pretty sure that I include files only once, I don't need to use include_once. This way, you also know which include is where. For instance, you know that crucial files like db.php, or functions.php are located in config.php. Or you know that include views are located in controllers. That's pretty useful for me, I hope that helps you, too.

PHP - Best Practice for Passing Variables to Include Files

I like an object for this, as described in this MVC stack post. In a file called viewMenu.class.php,

class viewMenu
{
private $active_link;

public function __construct ( $active_link )
{
$this->active_link = $active_link;
}
//If the constructor doesn't work for you, try a "set" method.

public function view ()
{
$active_link = $this->active_link;
include_once ( "menu.inc.php" );
}
}

Defining $active_link inside the view method contains the variable scope of $active_link to within the method. Then call this code:

$aViewMenu = new viewMenu( $active_link );
$aViewMenu->view();

However, I'm nearly new to MVC in PHP, and I welcome reproach.

PHP best practices?

You don't need a "system" to do templating.
You can do it on your own by keeping presentation & logic separate.
This way the designer can screw up the display, but not the logic behind it.

Here's a simple example:

<?php 
$people = array('derek','joel','jeff');
$people[0] = 'martin'; // all your logic goes here
include 'templates/people.php';
?>

Now here's the people.php file (which you give your designer):

<html> 
<body>
<?php foreach($people as $name):?>
<b>Person:</b> <?=$name?> <br />
<?php endforeach;?>
</body>
</html>

What is the best practice to include header and footer in PHP?

There's multiple ways to fix this.

  1. Use a templating engine, like Blade or Twig.
  2. Create an autoloader

Blade will allow you to make a layout, which can look like this:

<html>
<head>
<title>App Name - @yield('title')</title>
@yield('css')
</head>
<body>
<!-- Include all your files -->
@php
include('myfile.php');
@endphp

<div class="container">
@yield('content')
</div>

@yield('js')
</body>
</html>

Then for every other page you can create a blade file that extends the layout.

@extends('layout.file')

@section('title', 'Page Title')

@section('content')
<p>This content will be placed in the container in the layout.</p>
@endsection

You can write an auto-loader by yourself or use a pre-written one. I'm not going to attempt writing one because I usually go with the pre-written ones.

This should give you an idea though.

PHP include file extension best practices

First of all, I totally agree with you when you say that all PHP files should have ".php" as a final extension ; two reasons for that :

  • as you stated, it helps prevent un-parsed files being fetched
  • it also helps with IDE/editors that do syntax-coloration based on filename : you don't have to configure it to consider ".inc" as a PHP file.

There are cases when I do otherwise, though ; the main reason for that is when I'm using a tool (CMS, Framerwork, library, ...) that has some rules about naming of files : I tend to follow those, even if I don't like them.

For instance :

  • With Drupal, I use ".inc", ".module", ".install", ...
  • With Zend Framework, I use ".phtml" for views scripts (HTML+PHP)

For files that contain classes, I don't like ".class.php" : I think it's kinda redundant ; I tend to use "MyClassName.php", and use this for autoload.

(BTW, that's what Frameworks like Zend Framework or Doctrine ORM recommend)

As a sidenote : you say you are not a big fan of autoloaders ; why ?

I use those as much as I can :

  • generally better for performance : only the code you really use is loaded / parsed
  • less code to write (no require/include)

Best Practice: include( or script src=

<script type="text/javascript" src="min.js"></script>

...is better, as the user's browser can cache the file.

Adding a parameter to the src such as the file's last modified timestamp is even better, as the user's browser will cache the file but will always retrieve the most up to date version when the file is modified.

<script type="text/javascript" src="min.js?version=20081007134916"></script>


Related Topics



Leave a reply



Submit