my query is about architecture and relation of classes, I would like to know better solution than I have implemented.
I have 4 classes , explaining one line for each
- core.php class which will have all common functions
- A.php class file which contains specific function for task A like writing on csv file A.
- B.php class file which contains specific function for task B like writing another csv file which is totally different from csv A.
- X.php class which is another task specific class.
Now come to general script which will use all 4 classes. so I am not understanding which class will extend which another class
No relation between A.php and B.php
But x.php must use all 3 classes core.php, A.php and B.php.
currently what I have done , I design one interface for core class, and x.php extend core class and implement interface. I design A.php and B.php as static classes.
Please guide
You don’t have to
extendclasses if they have nothing to do with each other. You can simply use one class inside another without those two having any relationship inheritance or interface-wise.You may structure it like this:
class Commonis your base class that every class inherits from and contains common code that all classes need, say code for logging or database access.class A extends Commonand is specialized for some CSV-related tasks. This class may or may not actually do anything by itself.class B extends Aand is even more specialized to some specific task involving CSV.class X extends Commonand is again some other task.Generally, if a class does almost the same but slightly more specialized or differently,
extenda common base class. If a class has nothing at all in common with another, just make them standalone classes. Useinterfacesfor polymorphism; it doesn’t sound like that’s what you need right now.