A
A
Anton R.2019-09-11 12:30:29
OOP
Anton R., 2019-09-11 12:30:29

Wrote a class but it looks like a normal function, is that normal?

In general, for my project (advertising board) I decided to file the ad promotion function as in Avito - bringing it to the top. To do this, I add two additional fields to the declaration in the items table:
- promotion (can be 0 or 1)
- promotion_time (promotion time, for example 7 days, set by counting from the current unix time + 604800 seconds, when checking, we compare the current time and recorded in the database)
And here, when displaying an array of ads, to check whether the ad is being promoted, I decided not to shove all the checks into my procedural code, but wrote a Promoter class that will check whether the ad is being promoted and whether the promotion period has expired:

class Promoter {
  
  private $item_id;
  
  public function __construct($item_id){
    
    $this->item_id = $item_id;
    // $this->item_id будем дальше использовать при запросе в базу в методе checkPromotion().
  }
  
  public function checkPromotion(){
    
    // Проверяем у item поле в базе, 0 или 1
    // Если после первой проверки значение 1 -> Проверяем дату продвижения, 
    // если срок продвижения истек то записываем в поле БД "0" и возвращаем "0", если нет то возвращаем "1"
    
    return $period == true ? 1 : 0;
  }
  
}

$checkPromotion = new Promoter(1);
echo $checkPromotion->checkPromotion();

And here the whole humor is that I have only one function in this class) Is it worth it to fence such a garden for this?
PS Do not judge strictly for the syntax and construction, I just comprehend OOP)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2019-09-11
@anton_reut

Separate classes are good practice. However, the check itself should be placed where it is used. In your case, this is not done quite right. The check must be placed in the class, declarations, but at the same time refer to the check of the promo class.
It will be something like this:

public function getPromotion(): ?Promotion
{
     return new Promotion($this->promotion_id, $this->promotion_time);
}

Next, we put the test method:
public function isPromoted(): bool
{
     return $this—>getPromotion()->isPromoted()
}

If your data for the method may be empty, then the condition may look like this
public function isPromoted(): bool
{
     return $this->getPromotion() ? $this—>getPromotion()->isPromoted() : false
}

I do not pretend to anything) I do so) It is good to create additional classes. You can even create a separate class for Id. It will be convenient to work in typing. And it is clear to everyone. And in general, everything will not be in the same class.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question