Opencart – PHP Coding (require_once)

This is the second part of what will be many articles on PHP coding as it involves Opencart.  This article is on a very common usage in php called require_once. I chose this because it is used in Opencart very ubiquitously and in general a very common PHP declaration.

The list of require functions:

  1. -include
  2. -include_once
  3. -require
  4. -require_once

What is require_once?

The last of the four functions used to include particular pages is the require_once, which is a combination of the require and include_once function. This will make sure that the file exists prior to adding it to the page if it’s not there it will throw a fatal error. Plus it will make sure that the file can only be used once on the page. This is very handy in many situations

This function is the most strict out of the four functions and is the function I use most when constructing the page. Going back to the example used in the include function the require_once function is what you should use when displaying things like the website header and footer. This is because you always want these files to be here if they are not here you want the site to error, and you only want these to appear once on the page.

Here is a generic example of why you would only want the include to show once on the page and why require_once can be useful:

require_once 'header.php';

<div id="content">


require_once 'footer.php';

In that example you really only want the header and footer to show one time.

Now let’s take a look at how a require_once is used in Opencart. Go to the following file: /index.php

Once in that file you will see quite a few require_once’s but find the following:

// Startup
require_once(DIR_SYSTEM . 'startup.php');

Since this is in the index.php it is sure to be called on “every page load”. Sometimes people use require_once to load entire templates into existing pages as well.

Important side notes to take notice of:

If your code is running on multiple servers with different environments (locations from where your scripts run) the following idea may be useful to you:

a. Do not give absolute path to include files on your server.
b. Dynamically calculate the full path (absolute path)

Use a combination of dirname(__FILE__) and subsequent calls to itself until you reach to the home of your ‘/index.php’. Then, attach this variable (that contains the path) to your included files.

One of my typical example is:

define('__ROOT__', dirname(dirname(__FILE__)));

Instead of:

<?php require_once('/var/www/public_html/config.php'); ?>

After this, if you copy paste your codes to another servers, it will still run, without requiring any further re-configurations.

Lastly, here is a nifty MOODLE diagram of what happens when you include a config file.


Looking for quality OpenCart Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , | Posted under OpenCart | RSS 2.0

Author Spotlight

Joe Stenhouse

I am a web application developer that specializes in PHP, JAVASCRIPT, MYSQL, HTML, and CSS. We manifest exciting potentials within the world wide web through means of innovation.

Leave a Reply

Your email address will not be published. Required fields are marked *