Jump to content

Orientation to build a correct class


Gurzi

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.