How to call the function via $this in a loop?

A class which is a function of output of all goods. Each product in the database has its value but the function get_price() performs various manipulations with the price
class Product {
# get the goods
public function get_all() {

 $all_products = SELECT * FROM ...

 foreach($all_products as $products) {
 $this->comission = 5;
 $this->base_price = 500;
}

 return $this;
}
}
# generated price
public function get_price() {
 $price = (( $this->base_price ) + ( $this->comission / 100 ));

 return $price;
}
}


in the end, the syntax should be like this

$product = new Product;

foreach($product ->get_all() as $item) {
echo $item->get_price(); // here it is necessary to apply the function get_price() for each iteratsii
}


But the design is not correct. get_price takes the value of only the first key of the array and applies this value to all. How can I apply the function in a loop? Thanks for the help
June 5th 19 at 21:53
2 answers
June 5th 19 at 21:55
Solution
For this to work it is necessary when removing from the database somehow to convert the data array in the Product object.
And collect an array of objects.

And you have some kind of kasha Malasha turns out.

This is senseless and merciless
$all_products = SELECT * FROM ...

 foreach($all_products as $products) {
 $this->comission = 5;
 $this->base_price = 500;
}

 return $this;
Thank you. Can you put an approximate algorithm? - brenden_Johnson commented on June 5th 19 at 21:58
,
all of this ORM is engaged, the bike build makes no sense.

But let's say so
$all_products = SELECT * FROM ...
$models = [];
foreach($all_products as $products) {
 $model = new Product();
 $model->comission = 5;
 $model->base_price = 500;
 $models[] = $model;
}

return $models;
- bailee_Stant commented on June 5th 19 at 22:01
grateful! I have a related question, given your experience podskazhite please on how to call the function get_price();

In a method of the class: $model->price = $this->get_price();
Or array in the output: $product->get_price();

Thank you - brenden_Johnson commented on June 5th 19 at 22:04
here's who prefer.
If the price is calculated on some criteria/parameters, it is logical through the function to get it, to make it clear that there is not just a column in the database.
If it is in a database is already processed, then of course to give directly through the property of "raw data". - bailee_Stant commented on June 5th 19 at 22:07
June 5th 19 at 21:57
I understand you are trying to re-invent collection, but it turns out bad. View how to implement collections what is more normal than your polutoratonny bike. Or write your own, but at least read something on the subject to begin with.

Find more questions by tags PHP