Jump to content

Moduleclass


Redroest
 Share

Recommended Posts

Hey people, I am trying to make OOP classes. Since I have made many cms websites I thought of making a moduleclass, but I am not sure if I am doing this in the right way. I dont even know for sure if this is the best/safest/OOP way. I always was and am somebody that first tries something and then posts it on a forum to see how other people react. Please don't hold back on killing my script when that is needed. Don't start about the security databaseconnection. This is called from another class which also handles security and stuff. I also know that the class is far from complete (looking at the tasks function)

<?php//modules.phpclass modulinator{  //$moduleMap will also be the tablename in the database	var $moduleMap;	//defaultModule will be set as mainpage or when a wrong module is selected  var $defaultModule;	//$connection contains the databaseconnection returned by another class	var $connection;  //Select a module based on ID number	function select_module()	{	  //ID number from url has to be a numeric value	  if(isset($_GET['MID']) && is_numeric($_GET['MID']))		{		  //select chosen module from database	  $sql = 'SELECT * FROM '.$this->moduleMap.' WHERE ModuleID="'.$ModuleID.'"';	  //Return error when there is one			if(!$Modules = $this->connection->query($sql))	  {		trigger_error('An error occured while loading a module: '.$this->connection->error);	  }			//No errors and a module is returned	  elseif($modules = $Modules->fetch_assoc())	  {			  include_once($this->moduleMap.'/'.$modules['ModuleMap'].'/index.php');				self::load_tasks($this->moduleMap, $modules['ModuleMap']);			}				}		//$_GET['MID'] is not set so select default module		elseif(empty($_GET['MID']))		{		  include_once($this->moduleMap.'/'.$this->defaultModule.'/index.php');			self::load_tasks($this->moduleMap, $this->defaultModule);		}		//$_GET['MID'] was not empty but there is no modulename with that value		else		{		  die('Invalid url, please return to the previous page.');		}	}	//every module contains tasks in the form of functions. 	//So if there is a function in the  members/index.php called register() 	//I simply have to make an url like: modules.php?MID=2&task=register	function load_tasks()	{	  //Check if task is set and if there is a function set with that name	  if(isset($_GET['task']) && function_exists($_GET['task']))		{		  //execute function, although I think that this is buggy since it also cannot contain parameters 			//(maybe some help here)			call_user_func($_GET['task']);		}		else		{		  //Any non-existing values/functions will call index() (set as default function in every module)		  index();		}	}}?>

database.php (simplified without class because I am still working on that one to)

<?php//Database connection$db = array (  'host' => $db_host,  'user' => $db_user,  'pass' => $db_pass,  'dbname' => $db_name);$mysqli = new mysqli($db['host'], $db['user'], $db['pass'], $db['dbname']);if(mysqli_connect_errno()){  trigger_error('Fout bij verbinding: '.$mysqli->error);}//Secure POST requestsfunction SecurePost($_POST){  foreach ($_POST as $key => $value)   {	  if(is_array($value))		{		  SecurePost($value);		}		else	  {	  $_POST[$key] = mysqli_real_escape_string($GLOBALS['mysqli'], $value);		}  }}//Secure GET requestsfunction SecureGET($_GET){  foreach ($_GET as $key => $value)   {	  if(is_array($value))		{		  SecureGET($value);		}	  else		{	  $_GET[$key] = mysqli_real_escape_string($GLOBALS['mysqli'], $value);	}  }}if(isset($_GET)){  SecureGET($_GET);}if(isset($_POST)){  SecurePost($_POST);}?>

main.php

<?php$moduleMap = 'modules';$defaultModule = 'startpage';$modulinator = new modulinator;$modulinator->moduleMap = $moduleMap;$modulinator->defaultModule = $defaultModule;$modulinator->connection = $mysqli;$modulinator->select_module();?>

Edited by Redroest
Link to comment
Share on other sites

I reworked the class so that values are checked. Is the below class made in a correct OOP way?:

<?phpclass modulinator{  //$moduleMap will also be the tablename in the database	var $moduleMap = 'modules';	//$connection contains the databaseconnection returned by another class	var $connection;		function __construct($connection = '')	{		if(!file_exists($this->moduleMap.'/'))		{		  die('<strong>Filesystem error:</strong> Unable to find the modulemap!');		}	  if(empty($connection))		{		  die('<strong>Database error:</strong> Unable to find a database to load the modules!');		}	//$this->moduleMap = $moduleMap;		$this->connection = $connection;	}	  function select_module()	{	  //ID number from url has to be a numeric value	  if(isset($_GET['MID']) && is_numeric($_GET['MID']))		{		  //select chosen module from database	  $sql = 'SELECT * FROM core_'.$this->moduleMap.' WHERE ModuleID="'.$_GET['MID'].'"';		}		//$_GET['MID'] is not set so select default module		elseif(empty($_GET['MID']))		{	  $sql = 'SELECT * FROM core_'.$this->moduleMap.' WHERE Home=1';			$home = true;		}					//$_GET['MID'] was not empty but there is no modulename with that value		else		{		  die('Invalid url, please return to the previous page.');		}		//Return error when there is one		if(!$Modules = $this->connection->query($sql))	{	  trigger_error('An error occured while loading a module: '.$this->connection->error);	}	  //No errors and a module is returned	elseif($modules = $Modules->fetch_assoc())	{			include_once($this->moduleMap.'/'.$modules['ModuleMap'].'/index.php');			self::load_tasks($this->moduleMap, $modules['ModuleMap']);		  }			else		{		  if(isset($home))			{			die('<strong>Filesystem error:</strong> Unable to load the homemodule!');			}		}	}	//every module contains tasks in the form of functions. 	//So if there is a function in the  members/index.php called register() 	//I simply have to make an url like: modules.php?MID=2&task=register	function load_tasks()	{	  //Check if task is set and if there is a function set with that name	  if(isset($_GET['task']) && function_exists($_GET['task']))		{		  //execute function, although I think that this is buggy since it also cannot contain parameters 			//(maybe some help here)			call_user_func($_GET['task']);		}		else		{		  //Any non-existing values/functions will call index() (set as default function in every module)		  index();		}	}}?>//Call class:$modulinator = new modulinator($mysqli);$modulinator->select_module();

Link to comment
Share on other sites

I'm not sure if you can call the following OOP related, but they sure are (at least sort of) annoying.1. Instead of die()ing, consider throwing an exception. The user of your class should determine what to do in case of an error.2. if(empty($connection)) isn't a good enough check to make sure the desired database exists. It will only be enough to check that the user has entered any connection string (including an invalid one). If $connection must insetad be of another type (say, a mysqli object?), check it it is.But wait... what exactly are you trying to do again? Let 3rd party classes integrate with a system of yours? I suggest using interfaces or abstract classes for that. You define the kind of methods that each module must implement. Then, in your module loader/executer, you can safely assume they have the methods defined, and just execute them.Which one to use you ask? Using an abstract class will let you create default implementations for one or more functions, but will mean that modules will only have to inherit from it, and not from another base class, should they wish. Using an interface allows modules to potentially implement more than one interface that will enable them to register for different things in your program. However, an interface doesn't allow you to create default implementations. You can always use a combination of both of course.

Link to comment
Share on other sites

Ok I will read the above links to see what suits my needs. To answer your question. I am trying to make a modulesystem that includes modules. For example:In my filesystem is a map : (For example) modulesIn the modulesmap there are other maps: members, guestbook, startpage etc etc...Every module consists out of functions: function index(), function procesform(), function showform() etc...In the first place, my class has to work like a buffer: Get module on pageload, check if the module and the called functions exist and execute themI thought this would be the best way to handle such things and I also want it to be OOP so that others can use this class into there own system. But maybe I am not looking in the right way because when I search on "modules, OOP php class" it will have little good results

Link to comment
Share on other sites

By a "map", do you mean "a folder"? Because if so, you may simply load everyone with include or require, and in the module files themselves, end with a return, that returns a new instance of the class that this same file defines. For example, see the description of include (example #4 and #5).

Link to comment
Share on other sites

That was my original way of thinking synook. But I used this in an function oriented style and it gave the modulebuilder strict giudelines on how to make a module. So when I inserted something like a photoalbum that was used on many other websites it had to be rebuilded before I was able to use it.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...