3 Replies - 818 Views - Last Post: 04 November 2010 - 05:02 PM

#1 4D1  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 224
  • Joined: 22-October 09

How OO Should I Go

Posted 04 November 2010 - 08:07 AM

Hi Guys,

I am in the process of writing an object oriented PHP website, and I am a little confused as to what the typical industry trend is with regards to how OO to go? What I mean is for example at the moment I am creating the CMS and I am on the categories page, I have created classes and functions etc to handle various generic things, and I have a class and functions for getting categories from the database, this function returns an array of categories, should I create another function to build the tables, divs or whatever and display this, or should I handle it from here in the old fashioned way, considering the function will only be used once? Personally I think the old fashioned way would be easier to modify in the future, for example adding more category depths. Or is it better to create functions for everything?

What do you guys think?

Is This A Good Question/Topic? 0
  • +

Replies To: How OO Should I Go

#2 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3052
  • View blog
  • Posts: 4,573
  • Joined: 08-June 10

Re: How OO Should I Go

Posted 04 November 2010 - 11:22 AM

Personally I always seem to end up using a MVC pattern, so that's pretty much 100% OOP, with something like Smarty for the output.

But in general, I would try not to go so far in using OOP that it overly complicates the process. Sure, it's good to abstract repetitive things, but if the PHP code that is generating your HTML looks like DOM manipulation in Javascript, your overdoing it.
Was This Post Helpful? 2
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2486
  • View blog
  • Posts: 8,533
  • Joined: 08-August 08

Re: How OO Should I Go

Posted 04 November 2010 - 04:45 PM

I agree with Atli. You should write everything as if you will have to make changes to it six months after you've forgotten about it, because odds are that you will. Do something too clever and you will regret it. OOP can be used to make things easier to manage, and that's how you should use it.
Was This Post Helpful? 2
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9044
  • View blog
  • Posts: 33,555
  • Joined: 27-December 08

Re: How OO Should I Go

Posted 04 November 2010 - 05:02 PM

OOP is for just that- modeling Objects, their attributes, and their interactions. Your design should reflect this.

Quote

should I create another function to build the tables, divs or whatever and display this, or should I handle it from here in the old fashioned way, considering the function will only be used once?

Functions should be modular and reusable. So what I would do is have the function accept the appropriate parameters so it can build a the appropriate HTML given the appropriate parameters. For example, if you wanted to build a table with a header row, I'd have an array columnNames param, as well as the array for the Objects you want to output. Then build that table to populate the data one element per cell, with each Object's data on the same row. In this way, you allow for modularity and easy expansion in case the database is expanded.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1