Serverside Webscripting [JLW322]

06.code
organization

Include & Require

Include

  • Include the contents of one PHP file into another PHP file with the include keyword
    • Contents 01_include.php
      <?php
      
      	$x++;
    • Contents 01.php
      <?php
      
      	$x = 0;
      
      	include '01_include.php';
      	include '01_include.php';
      
      	echo $x;

Include once

  • Include the contents of one PHP file only once into another PHP file with the include_once keyword
    • Contents 01b.php
      <?php
      
      	$x = 0;
      
      	include_once '01_include.php';
      	include_once '01_include.php';
      
      	echo $x;

Include #fail

  • If an included file does not exist, you'll get a notice
    • Contents 01c.php
      <?php
      
      	$x = 0;
      
      	include '01_nonexistent.php';
      	include '01_include.php';
      
      	echo $x;

require & require_once

  • require does the same as include yet it won't give a notice but will result in a fatal error and stop the process
    • Contents 02.php
      <?php
      
      	$x = 0;
      
      	require '02_nonexistent.php';
      
      	echo $x;
  • I bet you can guess what require_once does
  • Best practice: always use require or require_once instead of include*

DRY

DRY?

  • DRY = Don't Repeat Yourself
    • Every piece of knowledge must have a single, unambiguous, authoritative representation within a system
  • If you have two files in your project which contain the same function/value you're not DRY, but WET (Write Everything Twice)
  • One of the ways to remain DRY is to work with includes

What needs to be DRY?

  1. Projectwide variables
  2. Recurring functionality

Projectwide variables

  • A variable that is constant and used throughout the project, yet is specific to the very project
    • The mailserver to use when sending an e-mail
    • The target mailaddress to send the various forms to
    • The database configuration data (server, username, password, databasename)
    • The API keys your website uses
    • ...
  • → You might also call this the configuration data

How to?

  • Create a config.php file and define all variables into it
    <?php
    
    define('MAIL_SERVER', 'localhost');
    define('MAIL_PORT', 25);
    
    define('DB_HOST', 'localhost');
    define('DB_USER', 'root');
    define('DB_PASS', 'Azerty123');
    define('DB_NAME', 'world');
    
    // EOF
    • Include this file everywhere, as the very first file to include and use the defined constants
  • config.php is the only file that may be changed when publishing a site onto a server

Recurring Functionality

  • Say you have a function isValidImageExtension() that checks whether a filename ends in .jpg/.jpeg/.gif
    • It'd be stupid to define this function in every file where you need it
    • What if you want to check for .png too? You'll edit all files?
  • A class definition — which itself is a step towards becoming DRY — only needs to be defined once

How to?

  • Create a functions.php and put all often used functions in it
    • Include this file everywhere, as the very first file to include and use the defined constants
  • If you define a class, define it in a separate file on disk
    • Include the file when necessary
Note: Until we start using libraries and talk about PSR-0 this way of organizing is sufficient

On Disk / Summary

Code Organization on disk

  • Make use of an includes/ subfolder to store config.php and functions.php in
    • Define your configuration data in config.php
    • Place commonly used functions in functions.php
    • Always include these two files in your project
  • Store all classes into includes/classes/
    • Include when necessary
Again: Until we start using libraries and talk about PSR-0 this way of organizing is sufficient

Questions?

Sources

ikdoeict.be