Gurzi Posted June 5, 2008 Share Posted June 5, 2008 Hello guys, at this moment i have 2 distinct classes, User and Product. Now i need a bunch of methods to list products( for example, products recently added, all the products, etc) . Where shall i define this methods ? Inside the class User ? Product ? within a new Class? Can you give me some advices ? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/108892-orientation-to-build-a-correct-class/ Share on other sites More sharing options...
Daniel0 Posted June 6, 2008 Share Posted June 6, 2008 Seeing as the methods are related to the products, logically they should be put in the Product class. Quote Link to comment https://forums.phpfreaks.com/topic/108892-orientation-to-build-a-correct-class/#findComment-559000 Share on other sites More sharing options...
mithras Posted June 6, 2008 Share Posted June 6, 2008 The user class represents (if instantiated) a specific user. The same for a product. Why do you ask the user what the recent added products are? If it's a good design, the user cannot know this information. Actually it's the same for a product. Why should a product know what other products are recently introduced? The best you can use the controller design for this situation: class productController{ public function createProduct(){ return new product(); } public function getProducts(){ $result = db::query( "SELECT * FROM products" ); return $result->fetchArray(); } public function getLatestProducts( $timespan = 3 ){ $result = db::query( "SELECT * FROM products WHERE date > DATE_ADD( NOW(), INTERVAL -1 MONTH) "); return $result->fetchArray(); } } class product{ private $name; private $id; } Now you abstract the product from the controlling product methods. Quote Link to comment https://forums.phpfreaks.com/topic/108892-orientation-to-build-a-correct-class/#findComment-559131 Share on other sites More sharing options...
cngodles Posted June 25, 2008 Share Posted June 25, 2008 mithras: You use the following in your example: $result = db::query( "SELECT * FROM products" ); Is the db:query a common way to send a query, or is there additional code that goes along with that. I myself have been coding PHP for 5 years, and I'm just now getting into OO PHP, and I'm trying to both grasp and justify it's use. Thanks! Clint Quote Link to comment https://forums.phpfreaks.com/topic/108892-orientation-to-build-a-correct-class/#findComment-573732 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.