Page 1 of 1
When are classes to modular? How many classes are too many?
Posted: Wed Mar 10, 2004 10:44 pm
by penguinboy
I like small effecient classes that do one specific task;
but I worry that small classes could == too many classes.
Is there a time when a class can be too modular?
Posted: Wed Mar 10, 2004 11:49 pm
by penguinboy
Looking at
this; I realize I'll problably never have too many clases...
Posted: Wed Mar 10, 2004 11:59 pm
by McGruff
Really it depends on what the class has to encapsulate.
As a general rule - with plenty of exceptions - 6 or 7 lines is a nice size for functions and six or seven methods is a nice size for classes. I've got plenty that are smaller or larger than that though. Smallest are a bunch of validators with only 5 or so lines of code which actually does anything. I find I've got 30-40 separate classes per script on average (you can find out approximately how many with printR(get_declared_classes()) - this is just what's been declared: could have more than one instance of a class).
It's always good to refactor ruthlessly till you hear 'em start to squeal.
Take a look at
Eclipse for a good example of responsibility driven design.
Posted: Thu Mar 11, 2004 1:23 am
by penguinboy
Another question:
classes within classes
will this set up work?
is it 'proper' ?
Code: Select all
<?php
class master
{
var $debug; // debug class
var $example; // example class
function master()
{
$this->debug = new debug();
$this->example = new example();
}
}
class debug
{
var $log; // array of debug messages
function debug()
{
$this->log[] = 'Debug started';
}
function append_log($log)
{
$this->log[] = $log;
}
}
class example extends master
{
var $debug;
function example()
{
$this->debug->append_log('Example Started.');
}
}
?>
Posted: Thu Mar 11, 2004 4:50 am
by timvw
I don't think you want another $debug in your example class, as master already has one..
Posted: Thu Mar 11, 2004 8:13 am
by penguinboy
But how would you access it?
This example
here.
Looks like you have to 'pass' the extended var.
It is a PHP5 example though, so I couldn't be wrong...