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">

</div>

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)

Hints:
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:

<?php
define('__ROOT__', dirname(dirname(__FILE__)));
require_once(__ROOT__.'/config.php');
?>

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.

Moodle_startup

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

Tags: , , , | Posted under OpenCart | Leave a comment

Increase Upload File Size in Joomla

Joomla has a preset size for each file you want to upload into your Media Manager, this size is set to 10 Megabytes (MB) but you can increase it easily by following a few simple steps. In this article I will explain how to increase upload file size in Joomla. Learn More

Tags: , , , , , , , , , , , , , , , , | Posted under Joomla | Leave a comment

Prestashop: Upgrade-safe template modifications

If you have ever been frustrated because your template modifications got lost after upgrading, here is the final, bulletproof solution for upgrade-safe PrestaShop template modifications (with overrides!).

Learn More

Tags: , , , | Posted under PrestaShop | Leave a comment