0
I am studying POO in PHP and came across an example that I did not understand:
The part of the code I don’t understand is because the attribute $produtos
this being
passed/calling the print() method on $produto->imprimir()
and tbm $usuario->imprimir();
the example is to add the $r variable to the output of these two excerpts.
How the attribute is calling the method and how this call works?
follows the code:
<?php
class Compra {
public $id, $produtos, $usuario;
public function cadastrar(array $produtos, Usuario $usuario)
{
$this->id = rand(0, 1000);
$this->produtos = $produtos;
$this->usuario = $usuario;
}
public function imprimir()
{
$r = "Compra id" . $this->id . "<hr>";
$r .= "Produtos" . "<br>"; #ESSA PARTE
foreach ($this->produtos as $produto) {
$r .= $produto->imprimir(); #ESSA PARTE
}
$r .= "<hr>";
$r .= "Usuario" . $this->usuario->imprimir(); #ESSA PARTE
return $r;
}
}
It cleared up, guys, I get it. i must create a Product class that implements the print() method; in this case it is part of the exercise, now that I have understood what must be done. thank you very much!
– Junyor Silva