Answer the question
In order to leave comments, you need to log in
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();
Answer the question
In order to leave comments, you need to log in
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);
}
public function isPromoted(): bool
{
return $this—>getPromotion()->isPromoted()
}
public function isPromoted(): bool
{
return $this->getPromotion() ? $this—>getPromotion()->isPromoted() : false
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question