SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Looking for advice on my framework structure

    Hi,
    I posted a thread in another forum on this yesterday, but I didn’t really get to the point very well and, well, my question didn’t really make much sense, so here is a second try.

    I have built a simple framework, which is based on MVC (although that’s not what my question is about). The way I have done it is that there is a Request object that is created first, this decides (based on the URL) what “module” to use (so there is a "site" module and an "admin" module), each module is stored in a separate folder. The Request class loads the modules main class and then the module loads the controller the controller loads the view etc…

    The thing that I feel is wrong with it all, is that everything is linked to everything else, for example the controller needs to use the database object that the module created, so when the module creates the controller it parses $this, and in the controller the module object is saved to $this->module. Then when the view is created, it needs to know where the template files are, so I needs access to the module objects $path variable, so the view has to be parsed the module object as well.

    Basically everything is linked to everything else, and it feels horrible, but I can’t see any other way to do it.

    In another place I have some simple “utility” classes (for example a utility for generating and sending an e-mail message). Because the e-mail utility needs to use files from the modules folder (and as there are multiple modules it needs to know what module to work with), when I create it, I again parse it the module object, as it is that that contains the path variable.

    Does anyone have any advice, or can suggest a good tutorial on how you should structure you objects and things. I am more than happy to rewrite the whole thing from scratch, I just don’t know which direction to go in.

    Thanks,
    Jack

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Sounds like you simply need a Registry to store the global data (database object, paths, etc.).
    Saul

  3. #3
    SitePoint Addict rvdavid's Avatar
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    233
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there,

    I'm assuming that you are using one bootstrap file (index.php) as an access point to your application.

    It seems as though you have some mixed up responsibilities. The excessive coupling that you are experiencing is due to an object being responsible for too many things. A general rule of thumb I like to follow is that one object does one thing and does it well.

    The Request Object

    Generally, a basic request object encapsulates request data ($_GET, $_POST, $_COOKIES), allows access and mutation through getter and setter methods.

    What you call a "request object" is more like a dispatcher.

    A Dispatcher object could read the request object and decide what module to load.

    This refactoring make sense as it separates module dispatching from request data.

    The Module Object

    What exactly does the module object do? From your description, it seems like it's a context object which contains mixed data. (Database Connection, Module Path information).

    You could decouple the database object and encapsulate the module information into a config object which could be initialised to contain module specific data and passed through the system.

    This makes sense because as it stands, you are passing around a database object created within the module object to objects which might not really need the database object (Like email, xmlreader... whatever.) and even if your existing setup does require the database object, you'd want to have the choice of passing or not passing the database object.

  4. #4
    SitePoint Addict rvdavid's Avatar
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    233
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by php_daemon View Post
    Sounds like you simply need a Registry to store the global data (database object, paths, etc.).
    I agree, although it wouldn't solve the coupling problems that is being experienced, a registry object would be a nice way of bundling global data and perhaps a semantic way to deliver database, request and config objects throughout the app.

  5. #5
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I make extensive use of the Visitor to separate responsibilities within my framework. In fact for the most part, a lot of the functionality hinges on this pattern. I couldn't have done it other wise without the Visitor.

    On the point of your request I too would say that you are mixing responsibilities as well, as noted earlier


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •