• 0

Question

Hi,

Is there a way to create the following in PHP without using a big framework such as Codeigniter, Zend or Cake? (I found something called NaturePHP, but there was no configuration instructions and it didn't work....).

Anyway, how would I:


<?php
class HelloWorld extends Modules {
function Message() {
echo "Hello World";
}
}
?>
[/CODE]

  1. Create a auto-loaded class. Basically, I include the class and it is already loaded (So i don't have to do new Class() ;). There are a few ways I think I could do this, one of them being adding a $class = new Class(); at the bottom of the class file.
  2. A module/plugin system. (Basically, what that NaturePHP is...). I want to have people to be able to place "modules" in a folder and the system automatically pick up that module, and it be acsessible. For example, the module would be as follows:

If i was to place that in the folder modules and call it helloworld.module.php, i can then once that module is included, call $helloworld->message();. Without having to first:

Load the module ($helloworld = new HelloWorld() ;)

Or include it... (require_once('module/helloworld.module.php') ;)

Anyone know how I would do this? If you need to me to clarify anything please ask below :)

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

&lt;?php
	define('MODULE_PATH', 'modules');
	define('MODULE_SUFFIX', '.module.php');

	function __autoload($module) {
		include (MODULE_PATH.'/'.$module.MODULE_SUFFIX);
	}

	$directory = opendir(MODULE_PATH);
	while (false !== ($entry = readdir($directory))) {
		if (is_readable(MODULE_PATH.'/'.$entry) &amp;&amp; fnmatch('*'.MODULE_SUFFIX, $entry)) {
			$module = substr($entry, 0, strripos($entry, MODULE_SUFFIX));
			parse_str($module);
	   	 $$module = new $module();
		}
	}
	closedir($directory);
?&gt;

Could this be the answer?

Link to comment
Share on other sites

This topic is now closed to further replies.