5 Replies - 5401 Views - Last Post: 02 October 2011 - 12:06 AM Rate Topic: -----

#1 Crazy_Learner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 145
  • Joined: 25-July 09

[Self-Resolved] PHP Module Loader

Posted 18 September 2011 - 12:25 PM

I am trying to create a module system, just to learn the mechanics of it all and what i came up with is this class, but it doesn't seem to work, but when i remove it from the class version it works fine. Any ideas?

ModuleLoader.php (Class Version)
<?php

/*
		           _______________________________________________________
		  ________()                                                     ()________
		  \       ||        Copyright (C) 2011 By Michael A. Roberts     ||       /
		   \      ||             ModuleLoader.php (Module System)        ||      / 
		   /      ||_____________________________________________________||      \
		  /_______()                                                     ()_______\
		  
	==============================================================================================
	Copyright 2011 By Michael Roberts
	==============================================================================================
	
	This class is designed to managed the installed module that will be used for the admin panel 
	to perform specific tasks regarding the server
	
	-------------------------------------------
	Implementation
	-------------------------------------------
	
	To implement this simply use the modular loading system class (this class) to load a module
	that needs to be used
	
	-------------------------------------------
	Design
	-------------------------------------------

	This is designed to load module by conforming the modules to a specific layout. The module 
	layout requires a specific naming convention for modules as well.
	
*/

require_once('./Errors/Exceptions/ModuleNotFound.php');

class ModuleLoader
{
	private $Module;
	private $ModuleClass;
	private $ModuleDirectory;
	private $ModuleExtension;
	private $ModuleBaseFile;
	
	private $Function;
	
	/**
	 *
	 * Create the module loader object that will allow for the information to be parsed and throw exception
	 * when it occurs to load modules
	 *
	 * ------------------------
	 * Parameters
	 * ------------------------
	 * 
	 * - moduleToLoad : The name of the module to load
	 * - functionToUse: The function to call from the module
	 *
	 * ------------------------
	 * Returns
	 * ------------------------
	 *
	 * - None
	 *
	 */
	public function __constructor($moduleToLoad, $moduleToLoad= 'index')
	{
		//
		// See if any information is contained in the module information. This will check to see if the module
		// name is set and if not will stop the processing
		//
		try
		{
			if(empty($moduleToLoad))
			{
				throw new ModuleNotFound('Module Name Missing');
			}
		}
		catch(ModuleNotFound $moduleMissing)
		{
			echo "module Missing";
		}
		
		//
		// Establish the variables to the module loader class and start loading the configurations of the module
		// loader
		//
		$this->Module = $moduleToLoad;
		$this->Function = $functionToUse;
		$this->SetupConfiguration();
		
		//
		// Attempt to find the module to be loaded. This module laoder will throw an exception if each
		// module is not in its own specific folder
		//
		try
		{
			//
			// Module should be loacated in the module directory under the module name followed by
			// the actual module
			//
			// Example: /Modules/HelloWorld/HelloWorld.php
			//
			if(!file_exists($this->ModuleDirectory . $this->Module . '/' . $this->ModuleBaseFile))
			{
				throw new ModuleNotFound('Module Missing');
			}
			
		}
		catch(ModuleNotFound $missingModule)
		{
			echo "module Missing";
		}
		
		//
		// Get The Function information that will be used when the module is loaded into the module laoder
		// By defaulting the value if its not valid
		//
		if(empty($this->Function) || $this->Function == '')
		{
			$this->Function = 'index';
		}
		
		//
		// Perform the loading procedures for the module. 
		//
		$this->LoadModule();
	}
	
	/**
	 *
	 * Special instructions for the destruction of the module loader. This module loader is not using any
	 * special instructions.
	 *
	 * ------------------------
	 * Parameters
	 * ------------------------
	 * 
	 * - None
	 *
	 * ------------------------
	 * Returns
	 * ------------------------
	 *
	 * - None
	 *
	 */
	public function __destruct()
	{
		
	}
	
	/**
	 *
	 * Setup the configuration for the module system loader. These are the settings that establish the 
	 * module base information. To change the directory type change the directory in this method.
	 *
	 * ------------------------
	 * Parameters
	 * ------------------------
	 * 
	 * - None
	 *
	 * ------------------------
	 * Returns
	 * ------------------------
	 *
	 * - None
	 *
	 */
	private function SetupConfiguration()
	{
		$this->ModuleDirectory = './Modules/';
		$this->ModuleExtension = '.module';
		$this->ModuleBaseFile = $this->Module . $this->ModuleExtension;
		
		//
		// Prevent Direct Access To Module Without The Module Loader Class
		//
		DEFINE('InModuleLoader', true); 
	}
	
	/**
	 *
	 * Load the module in to the system and execute the given function that was established within the 
	 * constructor method
	 *
	 * ------------------------
	 * Parameters
	 * ------------------------
	 * 
	 * - None
	 *
	 * ------------------------
	 * Returns
	 * ------------------------
	 *
	 * - None
	 *
	 */
	private function LoadModule()
	{
		//
		// include the module that will be loaded by the module loader
		// 
		include($this->ModuleDirectory . $this->Module . '/' . $this->ModuleBaseFile);
		$this->ModuleClass = new $this->Module;
	}
	
	/**
	 *
	 * Executes the given function from the loaded module. This method is called by the calling script 
	 * in order to delay the execution after the instance is created
	 *
	 * ------------------------
	 * Parameters
	 * ------------------------
	 * 
	 * - None
	 *
	 * ------------------------
	 * Returns
	 * ------------------------
	 *
	 * - None
	 *
	 */
	public function Execute()
	{
		//
		// Get The Function information that will be used when the module is loaded into the module laoder
		// By defaulting the value if its not valid
		//
		if(method_exists($this->ModuleClass,$this->Function))
		{
			$function = $this->Function;
			$this->ModuleClass->$function();
		}
	}
	
	public function ChangeFunction($Function)
	{
		//
		// Set the default information for the function if the function is not declared properly
		//
		if(empty($this->Function) || $this->Function == '')
		{
			$this->Function = 'index';
		}
		
		$this->Function = $Function;
	}
}

?>



FIXED
changed public function __constructor
to public function __construct


This post has been edited by Crazy_Learner: 18 September 2011 - 01:18 PM


Is This A Good Question/Topic? 0
  • +

Replies To: [Self-Resolved] PHP Module Loader

#2 Crazy_Learner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 145
  • Joined: 25-July 09

Re: [Self-Resolved] PHP Module Loader

Posted 18 September 2011 - 01:18 PM

no fix needed. fixed it on my own, see above post for fix
Was This Post Helpful? 0
  • +
  • -

#3 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1001
  • View blog
  • Posts: 3,561
  • Joined: 05-June 09

Re: [Self-Resolved] PHP Module Loader

Posted 21 September 2011 - 02:52 PM

I know you've resolved this, but dude - use docblocks, not, well, whatever you have going on there.

This post has been edited by RudiVisser: 21 September 2011 - 02:53 PM

Was This Post Helpful? 0
  • +
  • -

#4 Crazy_Learner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 145
  • Joined: 25-July 09

Re: [Self-Resolved] PHP Module Loader

Posted 29 September 2011 - 08:48 AM

View PostRudiVisser, on 21 September 2011 - 11:52 PM, said:

I know you've resolved this, but dude - use docblocks, not, well, whatever you have going on there.


docblocks is fine and dandy, but my code when finished is not public nor will it ever be as its not highly optimized code to begin with. its just to teach myself. So they way i see it, docblocks is not needed in this case. only when i release it to other programmers or other people do i need to have official documentation
Was This Post Helpful? 0
  • +
  • -

#5 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1001
  • View blog
  • Posts: 3,561
  • Joined: 05-June 09

Re: [Self-Resolved] PHP Module Loader

Posted 29 September 2011 - 12:15 PM

Errr, I meant to replace this monstrous rubbish that does nothing, helps no-body, and makes the code 7,000,000x larger:
	     /**
	     *
	     * Create the module loader object that will allow for the information to be parsed and throw exception
	     * when it occurs to load modules
	     *
	     * ------------------------
	     * Parameters
	     * ------------------------
	     * 
	     * - moduleToLoad : The name of the module to load
	     * - functionToUse: The function to call from the module
	     *
	     * ------------------------
	     * Returns
	     * ------------------------
	     *
	     * - None
	     *
	     */


IDEs actually read docblocks, they're a lot smaller and they make much more sense..
/**
 * Create the module loader object that will allow
 * for the information to be parsed and throw exception
 * when it occurs to load modules.
 *
 * @param string $moduleToLoad Name of module to load.
 * @param string $functionToUse The function to call in the module
 */


Any good IDE will parse this and give appropriate typehints and information when the user requests it. You can also, at any time, create a whole class documentation by using the phpdoc script.
Was This Post Helpful? 0
  • +
  • -

#6 Crazy_Learner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 145
  • Joined: 25-July 09

Re: [Self-Resolved] PHP Module Loader

Posted 02 October 2011 - 12:06 AM

Well thanks, i will keep that in mind if i use an IDE. Right now im using Notepad so code hinting isnt an option of the editor. but when i switch to a ide i will keep this in mind
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1