Greytree

TamWiki

For a mouse who is a packrat

Technology » PHP Includes
using include and it's various siblings

Summary:this is what goes at the top of the site

(redirected from Main.PHPIncludes)

PHP is a wonderful language to use for all sorts of things, not just web applications. One of it's great features is the ability to include other php scripts in the main script. This can lead to some interesting application architectures.

The include family

There are 4 types of include mechanisms:

  • include
  • include_once
  • require
  • require_once

These all do similar things, but there are some differences.

  • The _once directives will faithfully only allow you to include a file one time during the run of the script, thus saving you from having to worry about including something more than once in different scripts if they use the same features.
  • The difference between the include* and the require* directives is subtle. Require will issue a compiler error and fail if the file is not found in the include path, whereas include will issue a warning and continue.

Generally speaking, unless you are going to provide your own error handlers, on a production application that is open to users, you probably don't want to use require*, and use include* instead to provide graceful degredation if the features in the included script are not available. This is much preferrable than presenting a completely blank page to the user.

The Include Path

PHP will search it's include path to look for the filespec given in the directive. This include path is set in the php.ini file in the include_path entry. PHP will search this path, then look in the calling scripts directory and finally the current working directory. (This can make for some interesting patterns in include directives.)

Common code, utilities, modularization

The most frequent application of includes is to include other parts of the application. Any code that is used in multiple places or applications should be written as a separate chunk of php and included in the application. This is very appropriate for libraries of reusable code, and also for modularizing an application. It's quite a maintenance nightmare to have to wade through an application script that is miles long and having functions and variables all over the place.

In particular, doing object-oriented programming is when to make good use of includes. Your class is nicely enclosed in it's own php file which you can include in your main and then instantiate an object of that class.1

Single entry, with functions dispatched via includes

In this model, there is a single entry script, say index.php, for example.2 The single entry script will use include or one of it's siblings to pull in the various pieces of code to run the application, perhaps including certain files only if the current conditions warrant it.

This is a classic way of creating a controller for an MVC? architecture. The controller pulls in the appropriate models to execute functions, then pulls in the approprate views to display the output. All this can easily be done with includes.

Silly uses of includes

The include directives really have no notion of what it is they're including, they just do something interesting if the file is not there or is unreadable, otherwise they pass the contents of the file into the php interpretter. Thus, they don't do any sort of checking to see what is being included. Thus, you can do some silly sorts of things, like this:

<?php
header("Content-type: image/jpeg");
include("image.jpg");

This will work because the php interpretter is looking for instances of "<?php" and "?>" to do anything, which won't appear in the binary image file.

However, it's best not to do these sorts of shennanigans as it will be hard on the person coming back to maintain and fix something, should that occur3

The way to handle to above is to use readfile instead:

<?php
header("Content-type: image/jpeg");
readfile("image.jpg");

Readfile does not pass anything to the compiler, it simply reads from the file specified and sends the output to the client directly.


Tags: Categories: Articles

Recent Changes | Printable View | Page History | Edit Page
Page last modified on April 17, 2012, at 08:59 PM by ImportText?