Reputation: 9176
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?
Upvotes: 4
Views: 837
Reputation: 26132
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.
Upvotes: 4