Pastebin

The pastebin is a database of snippets. Snippets are actually small bits of code, in this case they can also be whole files though. For it’s highlighting, this pastebin uses Geshi.


Submit new snippet

Latest snippets

Moving-Copying-Deleting
by bonelifer
24 Jan 2013, 19:07

SQL Db Backup Script
by bonelifer
30 Sep 2012, 16:23

AutoJoinOnInvite-001.pl
by bonelifer
26 Aug 2012, 04:01

Class loader
by imkingdavid
26 Apr 2010, 19:33

Yahoo Status
by Deano
02 Jun 2008, 04:31

Class loader

Method to load a class

Snippet body:
  1. /*
  2. load - Loads the selected class file, initializes the class, calls main() method using $mode.
  3.  
  4. Parameters
  5.         $class = PHP class file relative to includes/classes/ -- NOTE: Do NOT include .php
  6.                         ALSO NOTE: The class name should be identical to the file name.
  7.         $mode = The mode to run; this allows the same class file to be used for multiple pages if needed.
  8.                 (Optional) Default: main
  9.                                 NOTE: all classes must have a main() method, as well as a "main" mode to default to!
  10.  
  11. Return - Returns the class in variable $class
  12. */
  13. function load($class, $mode = 'main')
  14. {
  15.         global $phpbb_root_path, $phpEx;
  16.         $class_file = $phpbb_root_path . 'includes/' . $class . '.'  . $phpEx;
  17.         // File does not exist; we end the script with a nice little error message
  18.         if(!file_exists($class_file))
  19.         {
  20.                 trigger_error('NO_CLASS_FILE');
  21.                 return;
  22.         }
  23.         else // File exists, so we move on.
  24.         {
  25.                 @include($class_file); // Include the file
  26.                
  27.                 // See if class exists in the included class file.
  28.                 // Because it should be the same as the file name, we can just use the class file name provided.
  29.                 if(!class_exists($class)) // If it does not exist, show a nice little error message and abort.
  30.                 {
  31.                         trigger_error('NO_CLASS');
  32.                         return false;
  33.                 }
  34.                 else // Class exists
  35.                 {
  36.                         $class = new $class;
  37.                         $class->main($mode); // Call the main() method using the $mode provided by the user
  38.                         return $class; // Return the class object
  39.                 }
  40.         }
  41. }

Copying code directly through the browser usually breaks the tabbing, so here you can copy the code to paste into your file-editor.

cron