Jump to content


Photo

Modular Systems?


  • Please log in to reply
28 replies to this topic

#1 Nolongerused3921

Nolongerused3921
  • Members
  • PipPipPip
  • Advanced Member
  • 142 posts

Posted 12 September 2006 - 10:15 AM

Well I've decided to take the plunge and rewrite my base code I use in all my projects, and I want to make sure to get it right this time and make sure its completely modular. But, quite honestly, I don't fully understand the allure, need, or what a modular system truly entails.

I'm open for advice on what features a modular system needs, and any articles you can point out for me :)

Thanks

#2 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 12 September 2006 - 10:18 AM

A Modular system doesn't 'need' anything :)

Before you even begin - I highly suggest you read up on Design Patterns, Test Driven Design and that you start completely from scratch with any project. Get some Use Cases together, then schematics, interface designs, then class diagrams, etc. etc.

When developing a system, very little time is actually spent coding. The bulk is designing.

#3 Nolongerused3921

Nolongerused3921
  • Members
  • PipPipPip
  • Advanced Member
  • 142 posts

Posted 12 September 2006 - 10:27 AM

Indeed, however I wanted to know what the community here feels is needed in a modular system or what they have in their own. Along with any articles they used to inspire their systems... I'm good at planning whatever I program out, I just need to know what I need (I need ideas) first :P

#4 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 12 September 2006 - 10:49 AM

A Framework. :)

#5 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 12 September 2006 - 02:58 PM

...then class diagrams,...


Got any resources to share on that? I've been looking for those.

BTW, I think this qualifies as an 'Application Design' question.

#6 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 12 September 2006 - 03:06 PM

For starters, I would extend any classes you can. For example, update the query method in mysqli to throw custom exceptions instead of placing an or die (...); (or what have you) after each query.

Use constants (short cuts), e.g.:

<?php

	### General.
	define('SELF', $_SERVER['PHP_SELF']);
	define('INC', 'includes/');
	define('GUI', INC . 'gui/');
	
	### Database.
	define('MYSQL_HOST', '...');
	define('MYSQL_USER', '...');
	define('MYSQL_PASS', '...');
	define('MYSQL_DB', '...');

?>

Use naming schemes.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#7 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 12 September 2006 - 03:34 PM

When going for a modular design, try to avoid 'hard coding' options.

Personally I use a file 'config.xml' to store all my options, that way the configuration (like installed modules) can be altered without changing the actual php code.

Given that you would have an class called 'config' to store/get configuration options, you could use that class to populate constants, or you could make a global instantiation of the class and assign the stored options to it's properties.

Example:
<?php
global $config;
$config = new Config('config.xml');
mysql_connect($config->MySQLHost,$config->MySQLUser,$config->MySQLPass);
?>
<?php
$config = new Config('config.xml');
define('MYSQL_HOST', $config->get('MYSQL_HOST'));
define('MYSQL_USER', $config->get('MYSQL_USER'));
define('MYSQL_PASS', $config->get('MYSQL_PASS'));
define('MYSQL_DB', $config->get('MYSQL_DB'));
mysql_connect(MYSQL_HOST,MYSQL_USER,MYSQL_PASS,MYSQL_DB);
?>
or:
<?php
//Upon construction, put all options in $options porperty
$config = new Config('config.xml');
foreach($config->options as $option=>$value){
	define($option, $value);
}
mysql_connect(MYSQL_HOST,MYSQL_USER,MYSQL_PASS,MYSQL_DB);
?>


#8 Nolongerused3921

Nolongerused3921
  • Members
  • PipPipPip
  • Advanced Member
  • 142 posts

Posted 12 September 2006 - 03:48 PM

For the loading, what I was thinking about doing is making the "main file" (All the module loading stuff), read a list of modules from a modules.php file (Which includes where they're located), and loading up ones that are important automatically (A priority system in modules.php?), and allowing each module to include other modules on its own.

Now this could lead to problems, but I have solved it (In theory :P), by making the module loading function (Which 'automatic loading' calls) add the module loaded into an array, and checking this array every time it loads a new module into memory - this way modules that need to access the mysql module, can call it in case they are called before MySQL is loaded automatically.

Its a bit confusing, but it sounds good... At least to me.


This is just a small portion of it, and I'd like to get some comments and suggestions and hear more ideas before I actually plan this all out in full :)

#9 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 12 September 2006 - 04:04 PM

For the loading, what I was thinking about doing is making the "main file" (All the module loading stuff), read a list of modules from a modules.php file (Which includes where they're located), and loading up ones that are important automatically (A priority system in modules.php?), and allowing each module to include other modules on its own.

Now this could lead to problems, but I have solved it (In theory :P), by making the module loading function (Which 'automatic loading' calls) add the module loaded into an array, and checking this array every time it loads a new module into memory - this way modules that need to access the mysql module, can call it in case they are called before MySQL is loaded automatically.

Its a bit confusing, but it sounds good... At least to me.


This is just a small portion of it, and I'd like to get some comments and suggestions and hear more ideas before I actually plan this all out in full :)


Just to make sure we're on the level here: with loading modules into an array you mean loading instances of classes representing modules into an array? If so, how do you propose to access the objects from other modules (objects)? You could of course make the array global. One of the things I run into often, is that when I load a diff class from one class from the constructor, I cannot operate on the global instance of the first class from the second class, because the object is not yet constucted. Be carefull what you put in constructors, watch your extends.

Or are you using procedural code, and are the files with functions simply included, and does the array hold the names of modules loaded? If so, you could do without the array and just check function_exists().

#10 Nolongerused3921

Nolongerused3921
  • Members
  • PipPipPip
  • Advanced Member
  • 142 posts

Posted 12 September 2006 - 04:09 PM

For the loading, what I was thinking about doing is making the "main file" (All the module loading stuff), read a list of modules from a modules.php file (Which includes where they're located), and loading up ones that are important automatically (A priority system in modules.php?), and allowing each module to include other modules on its own.

Now this could lead to problems, but I have solved it (In theory :P), by making the module loading function (Which 'automatic loading' calls) add the module loaded into an array, and checking this array every time it loads a new module into memory - this way modules that need to access the mysql module, can call it in case they are called before MySQL is loaded automatically.

Its a bit confusing, but it sounds good... At least to me.


This is just a small portion of it, and I'd like to get some comments and suggestions and hear more ideas before I actually plan this all out in full :)


Just to make sure we're on the level here: with loading modules into an array you mean loading instances of classes representing modules into an array? If so, how do you propose to access the objects from other modules (objects)? You could of course make the array global. One of the things I run into often, is that when I load a diff class from one class from the constructor, I cannot operate on the global instance of the first class from the second class, because the object is not yet constucted. Be carefull what you put in constructors, watch your extends.

Or are you using procedural code, and are the files with functions simply included, and does the array hold the names of modules loaded? If so, you could do without the array and just check function_exists().


Errr no you're completely misunderstanding me, what I meant is that it would load the module's NAME into an array... The function itself would load a module based on an alias, then add that alias to a list of modules loaded... It would be solely used to keep track of what is loaded.

And yes, your second statement is correct... However, I'd rather not use function_exists() as it would kind of defeat the purpose in my opinion, as it would require you to know a function from that module.

#11 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 12 September 2006 - 04:11 PM

one of the biggest unofficial no-no's in OOP is the use of globals :)

'good' OOP practice is pass values around as parameters when and where needed, not to blanket label something for all to see :)

<?php

$config = new XMLConfig('config.xml');

$db = new DataBaseClass;

$db->connect($config->db_host, $config->db_user, $config->db_pass);

//etc.
?>

For what you have posted above re: loading, I suggest your read up on the Service Locator pattern, Dependancy Injection and also the use of a Registry.

#12 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 12 September 2006 - 04:23 PM

one of the biggest unofficial no-no's in OOP is the use of globals :)

'good' OOP practice is pass values around as parameters when and where needed, not to blanket label something for all to see :)

<?php

$config = new XMLConfig('config.xml');

$db = new DataBaseClass;

$db->connect($config->db_host, $config->db_user, $config->db_pass);

//etc.
?>


I know use of globals is generally considered risky (although if you have a link to an article that explains the risks in depth it would be appreciated), but one has to consider performance.

If I create a new Config instance every time I need a configuration option, the class has to initialize equal those times. Meaning it has to read from the XML file at least a dozen times instead of once. So maybe it's better to use the class to define constants?

Furthermore, if you would want to avoid an object to become global, you have to take serious precautions!

For example, if I would put your code example in my index, both $config and $db would become global variables.

#13 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 12 September 2006 - 04:43 PM

... However, I'd rather not use function_exists() as it would kind of defeat the purpose in my opinion, as it would require you to know a function from that module.


But you intend to use the function, right? Wouldn't you need to know the name of the function in order to use it?  ;D

Just check for the first function you want to use, if it exists, you know the 'module' is loaded and it is safe to use the other functions in that module.

#14 Nolongerused3921

Nolongerused3921
  • Members
  • PipPipPip
  • Advanced Member
  • 142 posts

Posted 12 September 2006 - 08:39 PM

... However, I'd rather not use function_exists() as it would kind of defeat the purpose in my opinion, as it would require you to know a function from that module.


But you intend to use the function, right? Wouldn't you need to know the name of the function in order to use it?  ;D

Just check for the first function you want to use, if it exists, you know the 'module' is loaded and it is safe to use the other functions in that module.


Yes but I was kind of going for a no hassle, automatic, "just call the load function" functionality :P

#15 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 13 September 2006 - 10:06 AM

For what you have posted above re: loading, I suggest your read up on the Service Locator pattern, Dependancy Injection and also the use of a Registry.


Hey, thanks, that was of great use to me, especially the Registry pattern.  :)

#16 Nolongerused3921

Nolongerused3921
  • Members
  • PipPipPip
  • Advanced Member
  • 142 posts

Posted 13 September 2006 - 03:54 PM

I've been planning my engine out, but I've run into a bit of a problem... How should I handle permissions?
I was thinking having an internal module (Automatically loaded regardless) set to do permissions checks, and have each module set its own permission tables (Defines), but this leads to the (Nearly impossible but potential) problem of... What will I do when I want to access certain permissions from other modules?

My other idea was to put a SET permission table in the permission checker... But this kind of scares me, since I'd have to have a LARGE number of defines and could potentially run out of bitwise numbers.

Although, after a bit of thought... I suppose the latter would make more sense as the bitwise number would be stored in the database, in the same column...
Anyone have comments or suggestions on this?

#17 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 13 September 2006 - 06:10 PM

I've been planning my engine out, but I've run into a bit of a problem... How should I handle permissions?
I was thinking having an internal module (Automatically loaded regardless) set to do permissions checks, and have each module set its own permission tables (Defines), but this leads to the (Nearly impossible but potential) problem of... What will I do when I want to access certain permissions from other modules?

My other idea was to put a SET permission table in the permission checker... But this kind of scares me, since I'd have to have a LARGE number of defines and could potentially run out of bitwise numbers.

Although, after a bit of thought... I suppose the latter would make more sense as the bitwise number would be stored in the database, in the same column...
Anyone have comments or suggestions on this?


I'm kinda confused by this... Who (or what) exactly are you permitting (or not) to do what?

#18 Nolongerused3921

Nolongerused3921
  • Members
  • PipPipPip
  • Advanced Member
  • 142 posts

Posted 13 September 2006 - 06:54 PM

Bitwise permission system for, say, a user module... Can they login, are they banned, are they an admin, etc. - those kinds of permissions.

#19 newb

newb
  • Members
  • PipPipPip
  • Advanced Member
  • 454 posts

Posted 13 September 2006 - 07:10 PM

usergroup permissions can be done without bitwise. they can be done in sessions and stuff, i think.

#20 Nolongerused3921

Nolongerused3921
  • Members
  • PipPipPip
  • Advanced Member
  • 142 posts

Posted 13 September 2006 - 08:09 PM

usergroup permissions can be done without bitwise. they can be done in sessions and stuff, i think.


How exactly can an optimized Bitwise-like system be achieved with "sessions and stuff"?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users