views:

47

answers:

1

This project is in PHP, but is fairly language agnostic. I'm building a project that has the following MVC format

Main_Class
Main_Class_Common
  --> Class_A
  --> Class_B
  --> Class_C
  --> Class_...
  --> Class_K

Original requests go to the Main_Class which then includes the appropriate main sub class. The majority of a request will only need to access data in it's own class. However, there is a small bit of cross-class data. All sub classes need access to shared common functions in Main_Class_Common at all times.

For example,

Main_Class
Main_Class_Common
  --> Class_Projects
  --> Class_Clients

Usually, Class_Projects never needs data from Class_Clients except in one function it might need to call $class_clients->get_client_details($client_id); and the same occasionally happens in the reverse.

But both classes need to be able to call $main_class_common->clean_input($myinput);.

Currently, I'm using Class class_a extends main_class_common() for access to shared functions which works perfectly. But is the best way to access Class_B from Class_A to just access the global $class_b variable occasionally or should I move the possible shared functions into the Main_Class_Common even though they are used only occasionally?

The project right now is in the early stages of dev, so the current implementation can be changed easily.

+1  A: 

The repository pattern would fit in here. Create a Class_ProjectRepository class and Class_ClientRepository class, that contain methods such as get_client_details($client_id).

The MVC controller classes will make use of the repositories. For example, both Class_Projects and Class_Clients can use the Class_ClientRepository to retrieve client details.

The repository classes will build the query and call your DAL. Additionally, if you need actual domain objects such as Class_Client in your controller, you should also make the repositories responsible for transforming the DAL result sets into domain objects.

Niels van der Rest