tags:

views:

135

answers:

4

Hi,

I'm starting to write a application in php with one of my friends and was wondering, if you have any advice on how to implement module support into our application.

Or is there a way how to automatically load modules written in php by a php application? Or should i just rely on __autoload function?

And we don't need plugins that are installable via a web interface, we just need some clever way to associate web pages of our project to some classes (they will render the page) so index.php can call the right class and retrieve it's generated sub-page.

And we are not using any kind of framework, for now at least.

A: 

It sounds like you are looking for a way to organise the different tasks that each page needs to carry out. In this case, take a look at the MVC pattern. It provides a simple way to seperate your data access (models) and how you render/present information (views).

You can map pages to functions with ease. If you store the information in an array of mapped values, and then use a function to compare the requested URL with each of the URLs in the array. Such an array could look like this:

$urls = array(
    '/' => 'index',
    '/aboutus/' => 'aboutUs',
);

There are several articles that discuss how to implement it in PHP in a couple of hours. This article is a very simple guide. I am not a fan of their use of the registry pattern but reading through should provide you with enough information as to how you can implement it yourself.

Pheter
A: 

Re your comment: Autoloading in conjunction with strict file naming would be totally enough in this case IMO.

You could define a specific namespace (not in the new PHP 5.3 namespace way, just in the sense of a common prefix to names), e.g. Module_* for module class names.

You could then organize your modules in directories with class files that contain exactly one class definition, for example:

/modules/Mail/index.php   // defines class Module_Mail
/modules/Database/index.php   // defines class Module_Database
/modules/Image/index.php   // defines class Module_Image

Your autoloader function would then, whenever a Module_* class is requested:

$Database = new Module_Database("localhost", .....);

include the correct file from the right directory.

That's the way e.g. the Zend Framework does it, and does so pretty well.

Consider choosing a more specific namespace than Module_ to assure interoperability with other scripts and applications if that is an option in the future.

Slightly related: In a PHP project, how do you organize and access your helper objects?

Pekka
A: 

All you need here is an MVC architecture, with one Controller class associated with each "module".

If you are not against using a framework, go for Zend MVC

It allows you to have the following principle :

mexique1
+1  A: 

Regarding autoloading, you can also use spl_autoload_register, to define several (more than one) autoload functions, so each module can set up it's own implementation of autoloading.

WishCow