Jump to content

OO Design...


ajlisowski

Recommended Posts

Hi everyone, I have a quick question about how to design part of my system.

 

I have a intranet system I am developing. I have developed a number of systems already and now we are making a portal to tie them all together under one interface.

 

I have a module class which handles all activity regarding an individual module. Adding, removing, editing, adding/removing security groups.

 

Now, basically, I want to develop a class that handles actions not related to individual modules, but all modules. So, basically displaying modules to the portal page, determining what modules it should display based on security group etc.

 

What functionality should this class have and what should it be called? Should the class that grabs the modules from the DB and the class that displays them be the same? or should I seperate data handling from view handling?

 

I also have a module_ajax class which handles any and all requests that may be made via ajax. It usually simply calls the module class to do any execution.

 

Perhaps I should remove the ajax class and instead have a controller or handler class which does a combination of the ajax calls and the newer calls I am going to implement?

 

As an aside: I always get confused in regards to MVC and objects such as this. Do I need to worry about MVC when it comes to individual objects such as this, or is that more something I should worry about when rendering pages specifically?

Link to comment
Share on other sites

Now, basically, I want to develop a class that handles actions not related to individual modules, but all modules.

 

I assume this is not so common that you would make it a method of the parent module class? So, how about plugins?

 

So, basically displaying modules to the portal page, ..

 

A module on a web page and a module in the system are two different things. You could also load the module data from a view helper that loads the required data from a model.

 

determining what modules it should display based on security group etc.

 

I would create a view helper that takes in an argument regarding it's location, that calls a model to load the required modules based on the security group policy currently applied to the user eg

 

$this->loadSectionModule('sectionRightSidebar');

 

Should the class that grabs the modules from the DB and the class that displays them be the same?

 

Always try to keep all components as loose coupled as possible. If it only supplies a part of the data you work on then you most likely don't need it and it would be best to define a mechanism that passes the information through to the class.

 

or should I seperate data handling from view handling?

 

The answer should be obvious.

 

Perhaps I should remove the ajax class and instead have a controller or handler class which does a combination of the ajax calls and the newer calls I am going to implement?

 

Your view by default returns HTML for a HTTP request, extend this so that for XMLHttpRequest's it returns XML or JSON.

 

Do I need to worry about MVC when it comes to individual objects such as this, or is that more something I should worry about when rendering pages specifically?

 

In normal circumstances you should only worry about the M as the C is part of your application layer and in most cases already part of some framework as is the V which focuses on the presentation layer and comes with the framework.

 

Make sure all classes rely on classes within their layer and not on classes from other layer's. You should use Behavioral patterns whenever your class from a layer relies on the behavior of a class in another layer for maximum flexibility.

Link to comment
Share on other sites

I appreciate the help, and everything you said is helping me learn a lot about using frameworks and MVC in php.

 

However, I think I accidently made things confusing by using words I shouldnt have. In fact, I probably have a much broader question about my entire design.

 

WARNING: This is going to be long and possibly show how bad of a programmer I am!

 

Basically, at this point, I need to know if my system is absolutely fubar or if I can continue with it as such.

 

I have developed a number of systems for my company in PHP. Pretty much as I develop, I learn more and more, so each system has probably gotten better as time has gone on.

 

I sort of created my own framework which I have expanded upon as I went on. At the time, I had no idea I was making a framework, I didnt even know what one was really. I simply developed a nice way for individual components of a system to be called and organized.

 

Basically it works like this:

 

There is a public/index.php file which loads a config file and an application top and bottom file. It then calls the indexController.php which looks for a model in the  GET and then fetches the correct controller/module/modulename.php file.

 

 

These php files call whatever classes they need. And then do any data handling and then call a views/module/modulename.phtml file to do any sort of displaying.

 

These files themselves are not objects, thus they do not extend any base controller or view class. This is probably very bad and I should likely rework that.

 

Now, I utilize a lot of ajax in my systems and as time has grown on I have developed a way to keep things organized for myself. Basically for any object that would be maniuplated by the user I have a number of components. obj_handler.js (a js object which handles all the javascript) obj.php and obj_ajax.php. obj.php handles all the data handling and what not for the object. obj_ajax.php pretty much acts as a mediator between the js and the php object, tossing data between the two.

 

From your post I believe I should likely change this obj_ajax to a more broad obj_handler.php and check for whether or not its a httprequest or not, and return the data as needed.

 

The thing I do absolutely horrendous right now is I do not use json or xml and I simply serialize my data as needed and code the returning js functions as needed. Most my calls follow the same logic, a success or error code, followed by a number of variables seperated by **. I realizing JSON or XML would likely be more accepted. But so far this works fine for me...

 

What my previous question was is as follows:

 

Each system Ive made so far has followed the same approach as described above. Now I am combining each system into one intranet. I have a portal system which allows an admin to add or remove these systems, and provide security groups access to them. This will change the landing page and navigation bar.

 

My general OO question was whether i should create an object to handle displaying these systems (i said modules before) on the landing page, and in the nav bar etc. Or if I should just grab the data from the object and display the modules manually on the phtml page.

 

Does this design seem decent? Or am I coding in a terrible, messy and ineffecient way? Ive begun looking into zend framework, but id likely have to recode a lot of my systems to do so. Which, would take a lot of time to be honest, and I doubt my company would want me backtracking so much and spending time "fixing" stuff they dont see as broken.

 

 

Link to comment
Share on other sites

There is a public/index.php file which loads a config file and an application top and bottom file.

 

This is not good as you mix application logic with presentation logic. However what you address is a common problem and has already been solved by Martin Fowler and is called the Two-Step View and goes like this:

 

abstract class ViewAbstract {
  abstract public function render($script);
}

class DefaultView extends ViewAbstract {
  public function render($script) {
    include('views/' . basename($script));
  }
}

class TwoStepView extends ViewAbstract {
  private $view;
  private $content;
  
  public function render($script) {
    $this->content = $this->view->render($script);
    include('two-step-view-templates/some-template.html');
  }
}

 

You would specify TwoStepView as your new view somewhere in your front-controller. All your code in the controller's remain the same they have no idea they are even using a Two-Step View eg:

 

before TwoStepView:

public function indexAction() {
  $this->view->data = $modelService->load($stuff);
  $this->view->render('something.phtml');
}

 

 

after TwoStepView:

public function indexAction() {
  $this->view->data = $modelService->load($stuff);
  $this->view->render('something.phtml');
}

 

This gives you an idea of how it internally works, the exact implementation is up to you.

 

It then calls the indexController.php which looks for a model in the  GET and then fetches the correct controller/module/modulename.php file.

 

For a model in the GET? Your controller should know which model to use and not load these from a query. Maybe you should take a look at http://en.wikipedia.org/wiki/Presentation-abstraction-control

 

whether i should create an object to handle displaying these systems

 

As it's an OO application, yes.

 

Does this design seem decent? Or am I coding in a terrible, messy and ineffecient way?

 

Does the OO-design look decent? No. Are you coding in a terrible, messy and inefficient way? Terrible, messy? Probably not. Inefficient? Not likely as you are used to the system and you get things done.

 

Ive begun looking into zend framework, but id likely have to recode a lot of my systems to do so.

 

Gain experience in your spare time, many have made the mistake to switch to Zend framework while not knowing it's limitations, get familiar, read books, be a professional and decide afterwards if Zend is what you need.

 

Which, would take a lot of time to be honest, and I doubt my company would want me backtracking so much and spending time "fixing" stuff they dont see as broken.

 

Which is why I recommend you gain experience in your spare time. If Zend framework is what you need (although from your descriptions I think CodeIgniter may be more your thing) you can convince management that although now sacrificing resources on the re-design will gain time-and-money in the future due to a flexible design and changes will be swift and trouble-free.

Link to comment
Share on other sites

I think part of my issue is I am trying to turn a very much non- MVC design that I created over a year ago into an MVC design, and its confusing me, and though I have cleaned up the design and make it more and more OO friendly over time, I am still no where near where a true OO design should be.

 

I should probably learn the basics of MVC before I try to shoe-horn stuff into what my understanding of it is.

 

Everything I read generalizes the concepts a bit too much and I end up with a half understanding.

 

Im not even sure what role the model even plays in the whole thing.

 

Like, lets have a use case where a user deletes a photo from a photo gallery.

 

First the user browses the photos determining which one to delete.

 

In this case, the gallery controller would have a browse case, which it contacts the database and gets the photos for the current page and category or what not.

 

It would then call the browse view which would display the photos however. What "logic" should be done by the control view? Should it loop through the array of images, parsing the data and displaying it as needed?

 

So now the user sees all the photos and they click the delete button on one.

 

That gallery controller would have a function for the delete case.

 

It would then access the database and delete the row.

 

It would then call the gallery delete view which would show a message about the successful deletion. Or perhaps it would call the browse view again and show the message there with the newly updated gallery??

 

Thats the type of stuff I dont get.

 

Also, I would assume I would want to have a photo object which itself handles the deletion, updating, adding of images to a photo. And perhaps even the display of said photo. How does this fit into the MVC design?

 

How does ajax fit in? The controller gets an ajax call and then passes the data back to the javascript in JSON for it to manipulate the dom?

 

Or could you have the controller get an ajax call, then contact the view, serialize the html and pass that back to javascript?

Link to comment
Share on other sites

Everything I read generalizes the concepts a bit too much and I end up with a half understanding.

 

Let me first start off by saying that MVC is not OO-specific. In simple applications you enter http://domain/about-us.html as a URL in your browser (request) and you get the contents of the about-us.html page returned (response). You made a request to view about-us.html it was handled on the server and the contents of the about-us.html page were returned.

 

In an OO-context you want to map this request to an object (Controller) so that it can take the necessary actions (Models) and return a proper reponse (View).

 

This is all nice and dandy but how do I initialize a Controller upon receiving a request? I can't map a URL directly to it so I need some other means.. the FrontController is born:

 

A controller that handles all requests for a Web site.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.