# Neatest way of including a header and footer on every page

I just got into Object Oriented Programming, and it made me think about certain code how I can make them as efficient as possible.

Right now I am just including my header and footer like the following:

PHP

require_once 'core/init.php';

//Big block of content goes here

require_once 'includes/footer.php';

I wonder if this is the neatest / most efficient way of doing that?

According to a PHP blog post, yes this is exactly the way that it should be done. Even w3schools (whom you normally shouldn't trust too much since they're not related to the real w3 at all) recommends it.

I like that you are using require instead of include. I am not so sure if you really need the _once though.

I recommend you read the documentation of the include function (which also applies to require, require_once and include_once) to make sure that you really are aware of how it works. Note that if you are using any PHP scripts inside the included files, any global variables (which you should try avoid using too many of overall) also gets included to the calling script.

If you are working in PHP use a framework that has MVC such as Laravel(up and coming and popular), Symphony, Yii, Codeigniter(defunct), etc.

Each of them will enable you to use some reuse mechanism.

Laravel itself uses a template PHP called blade. There are several other template mechanism such as Twig that enables you to template your views to even add more granular way and cleaner.

If its a single file system that you must what you are doing is fine because you are using require_once it enables you to call other files and you can modify that file and it will reflect any other files that references it.

Although this is a great way of automatically including headers/footers in each page, you should call a function from within the header/footer script, as you will sometimes need to include additional content. For example:

Displayed page

require_once 'core/init.php';

$foothead = new foothead();$foothead->showheader('AwesomePage','<script src="somescript.js" />','<link rel="stylesheet" href="othercss.css"/>','<!--random header content-->');

//Big block of content goes here

$foothead->showfooter('<!--Other footer stuff-->'); foothead.php public class foothead { function showheader($title="hello",$scripts="",$css="",$other="") { echo "<html><head><insert your usual header stuff>{$scripts}{$css}<title>{$title}</title>{$other}"; } function showfooter($stuff="") {
echo "<your footer {$stuff} (goes in somewhere) >"; } } This obviously isn't the SIMPLEST way, but it is quite neat. While writing my website, I noticed (too late) that many of my pages required additional scripts/css. By using that ^ script, you can make sure your header/footer will be ready in case you need any additional things included. • I like how you did it with the methods and that you just send the variables as parameters with the method. – Kid Diamond Apr 3 '14 at 9:48 This is the fastest method I know of that does not include pasting your code on every page. Also include is faster than require / require_once / include_once. However I don't think this is the way to go about it. I completely disagree with INIT / GLOBAL / STATIC. If you really intend to do this in an OOP fashion you can't program like a php4 programmer any more. Instead you should focus all your attention on making your application run as a series of components that work as independently as possible from one another. In other words: 1. No init / setup functions / methods / files other than autoload.php and bootsrap.php in classes. Shift logic towards factory classes and separate classes. 2. No complicated code / logic in constructors. Constructors should simply set up properties. Try to convert that logic out of the class as another object. (ie config object or a tiny / micro object with only 1-2 responsibilities). 3. No configuration files that the app should depend on. Instead have the app run on configuration objects that can be loaded with information from configuration files. Using this design philosophy the actual printing out of output should be delegated to a class of it's own. Adding a header / footer / sidebar / etc should be dynamic and configurable. This is all very inefficient and creates many lines of code to do simple tasks. I think you should first build your app never even considering performance until it has been completed. After your app if fully built and tested. You can then go about searching for bottlenecks to try to optimize. Optimization during development will only hinder you and create more problems than solve them. You could also give register_shutdown_function() a try. bootstrap.php include 'autoload.php';$Configuration = new Configuration({
'DATABASE' => new ArrayConfig(new File('./config/db.php'))
'TEMPLATE' => new YAMLConfig(new File('./config/template.yml'))
//......
});

$ModelFactory = new ModelFactory($Configuration);
$TemplateFactory = new TemplateFactory($Configuration);

register_shutdown_function(function(){
extract(\$_GLOBALS);
include 'footer.php';
});

index.php

include 'bootstrap.php';
//......