tags:

views:

258

answers:

2

I'm building a freelancing site (think scriptlance) which has listings of projects both on the home page and the listings page. I thought it would be a good chance to implement OOP techniques.

I was thinking I would create a project class which among other things would have a function to echo the relevant contents for the home and listing page. I would like some advice on my proposed method.

I would create a class of something like:

class project {

    ...
    public function PrintSmallListing() {    
        echo "<div id="smallListing">
                  <h2>Listing Title</h2>
                  <span>listing description</span>
                  ...
              </div>";
          }
     }

Then on the relevant pages I would execute a database query then loop through the result create an array of these objects based on the data from the query. I would then display the listing of projects by looping through the array of objects and calling the

This means my queries would not be contained within my classes, otherwise I would need to call a separate query for every listing.

So my question is, is this a good way to implement OOP in PHP, are there some followed practices when using OOP for this kind of problem? or how would you personally do it?

+4  A: 

Well, this is OOP, as you are using objects. Everything that uses objects is OOP.

But no, you are using it totally wrong. This kind of application will be impossible to maintain in the future. To change the page design - you will have to find 100+ files and make an appropriate changes in each of them.

If you want to create a really nice modular OOP site and get better with OOP, I would propose using some open-source MVC (model-view-controller) framework, like cakePHP.

The common practice that is also used in MVC is view separation. That means, your view should not depend on business logic and business logic should not depend on the view. This way, when time comes to change the design - you can just swap it. Also your business logic is not trashed with various html tags or rendering functions.

Max
I think it's a good idea to understand this concept not only in the php aspect of your application, but in the front-end as well. Look for some article about separating structure/presentation/behaviour like this one: (it's old but I think it gets the idea) /http://www.mercurytide.co.uk/news/article/separation-structure-presentation-and-behaviour/
GmonC
Nice article. Added it to my favorites. Btw, another good article on similar topic: http://martinfowler.com/eaaDev/uiArchs.html
Max
"Everything that uses objects is OOP." Looking at that single statement, I practically could not disagree more. http://stackoverflow.com/questions/656836/explanation-of-re-usable-structures-in-oo-php/656975#656975 http://stackoverflow.com/questions/1343619/real-world-oop-example-php/1343881#1343881 http://stackoverflow.com/questions/1197616/can-you-help-me-understand-php-classes-a-little-better/1197679#1197679
Peter Bailey
Maybe I am totally misunderstanding it, but OOP generally means "the one that uses objects". As author of this question mentioned - "a good way to implement OOP" is an actual problem, creating application that uses OOP is not hard. There is no discrete definition of what OOP is. By the way, wikipedia tells I am right: http://en.wikipedia.org/wiki/Object-oriented_programming
Max
That's my point. Practical OOP (not dictionary OOP) is about a lot more than just "using object". A PHP codebase littered with instances of stdClass doesn't make it an OOP codebase.
Peter Bailey
A: 

Max y dont you prefer zend framework... i worked on both cake and zend.. Zend is 10 times better than cake.. GO with zend ..any way i agreed with max,u should follow any one mvc architecture framework.

Rithin