Как в классе User реализовать метод который возвращает все продукты определённого пользователя из регистра продуктов?
<?php
namespace lesson2;
class User{
private $name;
private $balance;
function __construct($name,$balance){
$this->name = $name;
$this->balance = $balance;
}
public function getName(){
return $this->name;
}
public function getBalance(){
return $this->balance;
}
public function __toString(){
return $this->getName().' ';
}
public function giveMoney($user, $amount){
if($amount > $this->balance){
return 'Недостаточно средств!'."<br/>";
}
else{
$this->balance -= $amount;
$user->balance += $amount;
return "Пользователь ".$this->name." перечислил ".$amount." пользователю ".$user->name;
}
}
public function listProducts(){
}
}
abstract class Product
{
private $name;
private $price;
private $owner;
static private $products = array();
function __construct($name, $price)
{
$this->name = $name;
$this->price = $price;
}
public function setOwner(User $user)
{
$this->owner = $user;
}
public static function getProductIterator()
{
return new class (self::$products) implements \Iterator {
private $position = 0;
private $array;
public function __construct($array)
{
$this->array = $array;
}
public function rewind()
{
$this->position = 0;
}
public function current()
{
return $this->array[$this->position];
}
public function key()
{
return $this->position;
}
public function next()
{
++$this->position;
}
public function valid()
{
return isset($this->array[$this->position]);
}
};
}
public static function registerProduct($product)
{
if (in_array($product, self::$products, true)) {
echo 'Введены одинаковые продукты ' . '</br>';
} else {
array_push(self::$products, $product);
}
}
public function __toString(): string
{
return "Название: " .$this->name . ', Владелец: ' . $this->owner . ', Цена:' . $this->price . ';';
}
}
class Processor extends Product{
private $frequency;
public function __construct($name, $price,$frequency)
{
parent::__construct($name, $price);
$this->frequency=$frequency;
}
}
class Ram extends Product{
private $type;
private $memory;
public function __construct($name, $price, $type, $memory)
{
parent::__construct($name, $price);
$this -> type = $type;
$this -> memory = $memory;
}
}
///////////////////////////////////////////
$prod = new Processor("Intel",4500,3.9);
$user = new User("Maxim",1000);
$prod->setOwner($user);
Product::registerProduct($prod);
$products = Product::getProductIterator();
foreach ($products as $p) {
echo $p ."\n";
}