Posted on May - 24 - 2011
Class oriented programming
For well-managed small projects, class-oriented programming works very well, especially for scripting platforms like PHP, where you actually never save any object for later use.
Here goes my plan of class-oriented development:
1. Create classes for every Entity. So if you have entities like ‘category’, ‘person’, ‘product’, etc, you need to create classes with names Category, Person, Product and with filenames Category.class.php, Person.class.php, Product.class.php, respectively.
2. Every class of entities encapsulates all the functions that only communicates with its respective data-table. So model and a part of the controller go into that class.
3. Every entity class has some similar functions which we can reuse heavily! (only some replacement required). These functions are Get, Add, Remove, ChangeStatus, GetInfo, GetTotal . They have similar goals, so similar codes (fast and reusable!). So, there will be functions like Category::Add, Person::Add, Product::Add. They adds new entities of their respective type into the database!
4. Now the problem occurs with the relationships between the entities. Where you can put the functions which relates to many entities. It has some nice manageable solutions. Firstly, in a weak relation, keep the function in the weak entity class ( it’s more logical, as the every strong entity may not have a weak entity related with it). Secondly, for a relation between two strong entities, have the function in the subject entity ( what is a subject entity ? Recall English grammar 101). Thirdly, if a entity has a total participation in a relation, have the relational functions in that entity’s class. Call me for other cases.
5. You can put the classes in the ‘includes’ folder, or the ‘classes’ folder. Check my FW.


















