Home arrow PHP arrow Page 2 - Building Loader Apps in PHP

Start building loading programs with a require_once() function - PHP

Loading sources on the fly is one of the most common tasks that PHP programmers have to tackle during the development of web applications. This typical situation must be faced independently of the scale of the programs being created. This means a loader mechanism must be developed. Keep reading as we take a close look at these mechanisms in this eight-part article series.

TABLE OF CONTENTS:
  1. Building Loader Apps in PHP
  2. Start building loading programs with a require_once() function
  3. Building a basic file loader class
  4. The loader class in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
May 27, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I said at the beginning, the simplest version of a loading application can be built by explicitly calling some “include()/require()” functions, which are used per file requested. So, to recreate a typical scenario in which these functions are utilized, suppose for a moment that there are two basic files that need to be included within an application, that are defined as follows:

('sample_file1.php')

 

<?php

echo 'This file has been loaded with the require_once() function.' . '<br />';

?>

 

('sample_file2.php')

 

<?php

echo 'This file has been also loaded with the require_once() function.' . '<br />';

?>

As shown previously, the two sample files listed above are extremely simple to grasp, so I’m not going to waste your time explaining how they work. Nonetheless, since there’s a fictional application that needs to include the files to do its business, a typical loader file would look like this:

('loader.php' file)

 

// include first sample file via a require_once() function

require_once('sample_file1.php');

 

// include second sample file via a require_once() function

require_once('sample_file2.php');

Definitely, the definition of the “loader.php” file is very basic, but it does show in a nutshell a classic situation, where one “require()” function is called per requested file. In addition, the following output would be produced by the loader file:

This file has been loaded with the require_once() function.

This file has been also loaded with the require_once() function.

This would be quite possibly the most basic implementation of a file loading mechanism in PHP. Despite its simplicity, it works pretty well when building small-scale applications.

However, when it comes to developing larger PHP programs, things can get more complex. There will probably be multiple files using many includes. This situation can lead to dealing with tedious debugging and maintenance issues.

So, to prevent the vast majority of these eventual problems, it’s necessary to create a file loading system that works in a more modular way, and at the same time allows us to abstract the whole loading process a bit further.

This objective can be achieved quite decently by using a simple loader class. Therefore, in the course of the section to come I’m going to discuss how to build such a class in a few basic steps.

To learn more about how this procedure will be accomplished, click on the link that appears below and keep reading.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: