Forum core source dump (as of July 31)

lib/Module.class.php

Note: Didn’t make too many changes here.

/**
* Module class.
* Sets the default module functionality. All modules must inherit this class to be usable.
*
* @author Eksith Rodrigo
* @version 0.1
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @access public
*/

class Module
{
	protected $props;		// Stored properties passed by main class
	protected $database;		// Database object
	
	/**
	* Module constructor. Sets the database and presets.
	*
	* @param array $p Properties passed by Modules class (which in turn gets it from global configurations)
	* @param object $db Database object
	*/
	protected function __construct(&$p, &$db)
	{
		$this->props = $p;
		$this->database = $db;
	}
	
	/**
	* Set properties
	*
	* @param string $name Property name to modify
	* @param mixed $value New property value
	*/
	protected function __set($name, $value)
	{
		// No "isset" check as new values may be shared between modules.
		$this->props[$name] = $value;
	}
	
	/**
	* Get properties
	*
	* @param string $name Property name to get
	* @return mixed $props[$name] Property value, if it exists.
	*/
	protected function __get($name)
	{
		if(isset($this->props[$name]))
			return $this->props[$name];
		
		return null;
	}
	
	/**
	* Helper function to check if a property value exists.
	*
	* @param string $name Property name to check
	* @return bool true If the property exists or returns false.
	*/
	protected function __isset($name)
	{
		return isset($this->props[$name]);
	}
	
	/**
	* Helper function to delete a property value.
	*
	* @param string $name Property value to delete.
	*/
	protected function __unset($name)
	{
		if(isset($this->props[$name]))
			unset($this->props[$name]);
	}
	
	/**
	* Helper function to include custom JavaScript or CSS files.
	*
	* @param string $file Path to include (depends on preset globals passed to modules)
	* @param string $type File type. "js" For JavaScript or "css" for stylesheets.
	* @return string $path Formated <script> or <link> tags for JS or CSS extras.
	*/
	protected function libInject($file, $type)
	{
		$path = '';
		switch(strtolower($type))
		{
			case "js":
				$path = '<script type="text/javascript" src="'. $file .'"></script>';
				break;
			case "css":
				$path = '<link rel="stylesheet" href="'. $file .'" />';
				break;
		}
		
		return $path;
	}
}
Advertisements

6 thoughts on “Forum core source dump (as of July 31)

  1. Pingback: I have developer ADHD « This page intentionally left ugly

  2. This was a very interesting read for me!
    Although I have a question,
    I am a beginning php programmer, currently developing drupal modules on my internship, but looking to develop some sort of forum script/cms/framework/… . I’m not planning on actually making it public, it’s just a personal project to teach myself some things. So, my question…, I saw in your main.php you do:
    Core::getInstance and Modules::getInstance, is it the same as doing:
    $core = new Core();
    $core->getInstance(…); ? and did you have to define ‘Core’ somewhere or does php automatically recognise it as a class and searches for it (with the autoload function you wrote)?
    thanks in advance!

    You can contact me on my email if you want

    • Hi Jelle

      Yes, the basic idea is to let the autoload function load the class dynamically so you wouldn’t need to declare it elsewhere.

      Technically $core = new Core(); wouldn’t work in this case becuse the __construct function in that class is set to private.

      So you would always have to use the getInstance method when you want to create a new instance unless you’re calling new Core() inside the Core class itself.

  3. Pingback: (Snippet) Blocking IPs from a list file with PHP « This page intentionally left ugly

  4. Pingback: PHP Plugin/Module system | This page intentionally left ugly

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s