Page 1 of 1

Creating your very own PHP Plugin / Module System Rate Topic: ***** 2 Votes

#1 Keiran  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 06-December 09

Posted 05 April 2010 - 04:32 PM

Well Everyone needs a PHP Plug-in system if you want to keep up to date and stop modifying loads of code every time you get a new idea for your website. In this tutorial I will show you how to create a sweet and easy PHP Plug-in/Modules System.

What you Will Learn
  • How to Dynamically Load Plugins
  • The basics of OO PHP


Pre-Requisites
  • Basic Knowledge of PHP
  • An understanding of File Directory structures



What does OO Mean?
OO Means Object Orientated. This is the use of classes to control the way your PHP Script works. It is used mainly to recycle code and keep things much easier to read.

Example 1.
We have a class called HelloWorld.class.php It contains the following functions :
hello()
world()
__construct()

The __construct() Function will be called no matter what after we initiate our class as it is the constructor function of the class. Now lets look how the code will look for HelloWorld.class.php

<?php
     
  class HelloWorld {

     function __construct() { //Our construct function
         print "HelloWorld Class has been constructed <br />"; // This will print when we define our class
     }

     function hello() { // this is the hello() function
         print "Hello"; //Print Hello when we call this function
     }

     function world() { // Finally the world() function
        print " World"; //Prints World
     }

  }

?>



Now this is how a class gets called. It is fairly simple

<?php

include_once('HelloWorld.class.php'); // Include our class

$HelloWorld = new HelloWorld; //Load our class into a variable

$HelloWorld->hello(); // Say Hello!!
$HelloWorld->world(); // Say  World

?>



This will output the following

HelloWorld Class has been constructed 
Hello World



Simple Huh?

Now onto our plugin system.

The Module System Directory Structure
Well its really upto you how you lay it out. this is how I will be doing it for this guide.

<WebRootDir>
      |
      + module.php (This will be the file we call the modules from
      |
      + modules (dir)
           |
           + HelloWorld
           |      + HelloWorld.module.php



Now Lets Start some serious coding :)

Lets start with modules.php since it will be the largest file. We will be using $_GET variables to access modules. E.G yoursite.com/modules.php?module=HelloWorld

Now I assume you have already setup your php tags if not create a new file modules.php and add the PHP Tags.

Now we will use the Variable $_GET['module'] and assign it to a variable $module for ease of use. I will also setup some configuration variables for ease of use.

<?php

$module = $_GET['module'];

// Configuration \\
$module_base_dir = './modules/'; //Directory of modules (You can use absolute path)
$module_ext = '.module.php'; // Our Module Extension
$module_base_file = $module.$module_ext; // So this will show HelloWorld.module.php


// DO NOT EDIT BELOW THIS LINE UNLESS YOU ARE CONFIDENT IN PHP \\

DEFINE('IS_IN_MODULE', true); //Define that this is the module controller

if(!isset($module))
{
    die("No Module Selected!!");
}

?>



The IF statement I added was just to check a module was selected. Now we need a way to check if the module file and directory exist. We use the file_exists() function to do this.

Add the following after the if statement in the last snippet

if(file_exists($module_base_dir.$module. '/' .$module_base_file))
{
	if(isset($_GET['function'])) //Check if we have defined a function
	{
		$function = $_GET['function']; //Assign the function to a variable
	}
	else
	{
		$function = 'index'; // if no function assign function index
	}
	include($module_base_dir.$module. '/' .$module_base_file); // Include our module
	$moduleClass = new $module; // This Will launch our plugin constructor.
	if(method_exists($moduleClass, $function)) // Check our function exists
	{
		$moduleClass->$function(); //Call the function defined above
	}
	else
	{
		die("Function not found!"); // Show error message
	}
}
else
{
   die("Module Not Found!!");
}



now thats our module.php file lets create our modules.

In the modules folder create a new folder called HelloWorld inside that make a HelloWorld.module.php file. This will be our main module class. Make sure the class name matches the Case of the module name.

<?php
if(!defined('IS_IN_MODULE')) { die("NO DIRECT FILE ACCESS!"); }

class HelloWorld {

	function index() // This is our index function. It is called if we do not have a function defined
	{
		print("HelloWorld Module from Keiran's Module System");
	}
	
	function hello() // hello function called from modules.php?module=HelloWorld&function=hello
	{
		print("hello");
	}
	
	function world() // hello function called from modules.php?module=HelloWorld&function=world
	{
		print("world");
	}

}

?>


Download Source : thttp://keiran-smith.net/moduletutfiles.zip

if you use the files be sure to give me some credit :)

Is This A Good Question/Topic? 0
  • +

Replies To: Creating your very own PHP Plugin / Module System

#2 Guest_Anouar*


Reputation:

Posted 29 April 2010 - 02:45 PM

thanks for this nice tutorials
Was This Post Helpful? 0

Page 1 of 1