When is the right time to subclass?

Working on a database class that turned out to be quite large and likely to become much larger, I began to write many attributes and methods related to one particular aspect of the database (users, comments, pages, etc.). for example, about the time for subclassing a database class to several classes, each of which deals with their own aspect, and only the database class contains the absolute core functionality.

However, it is like doing extra classes and communicating with includes and pretty for nothing. The code is quite easy to maintain as is and divided (via comments) into the correct sections, and it does not look like a "large" class that can hurt performance.

So, I come to you: when is the right time, in your opinion and experience, subclasses? Not only in this particular case, but in general terms.

+4
source share
2 answers

I go by a logical grouping of methods and functionality.

+2
source

This is normal. I have 50 models that process all kinds of data (many tables).

The inclusion problem can be easily resolved by using a naming convention and using autoloaders.

For example, I call all my classes as follows: ClassNameModel.php and put them in a specific directory.

The autoloader will look like this:

 function customAutoloader($class_name){ $file_name = $class_name . ".php"; //Modelis if(substr($class_name, -5) === "Model"){ if(is_readable("/models/" . $file_name)){ require_once("/models/" . $file_name); } return; } } 

and in the main index or configuration or something else:

 spl_autoload_register('customAutoloader'); 

and no more...

0
source

All Articles