Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/108892-orientation-to-build-a-correct-class/
Share on other sites

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.

  • 3 weeks later...

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

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.