Dinesh Goyal
Dinesh Goyal

Reputation: 137

php classes relation and how to extend classes

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

  1. core.php class which will have all common functions
  2. A.php class file which contains specific function for task A like writing on csv file A.
  3. B.php class file which contains specific function for task B like writing another csv file which is totally different from csv A.
  4. 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

Upvotes: 0

Views: 160

Answers (2)

Tobias Sjösten
Tobias Sjösten

Reputation: 1094

This sounds like basic dependency management. I don't understand why you explicitly want to extend anything though?

What you probably should do instead is inject the needed dependencies. Instantiate Core, A and B separately before instantiating X with the objects it's dependent on.

Upvotes: 0

deceze
deceze

Reputation: 522626

You don't have to extend classes 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 Common is 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 Common and is specialized for some CSV-related tasks. This class may or may not actually do anything by itself.
  • class B extends A and is even more specialized to some specific task involving CSV.
  • class X extends Common and is again some other task.

Generally, if a class does almost the same but slightly more specialized or differently, extend a common base class. If a class has nothing at all in common with another, just make them standalone classes. Use interfaces for polymorphism; it doesn't sound like that's what you need right now.

Upvotes: 2

Related Questions