2
I am studying POO and still can not see, in practice, the difference between them.
Basically everything ends up in one class having as attribute the instance of another class. But if it is Departamento
and Professor
would be aggregation. If it is Aluno
and Professor
is more to the association.
What differs is only in theory and concept? In practice it is the same thing?
Here’s a practical example I did:
<?php
//Agregação
class Produto{
public $nome;
public $preco;
function __construct($nome,$valor){
$this->nome=$nome;
$this->preco=$valor;
}
}
class Carrinho{
public $produtos;
public function add_produto(Produto $produto){
$this->produtos[]=$produto;
}
public function print(){
foreach ($this->produtos as $value ) {
echo $value->nome;
}
}
}
$produto = new Produto("Teclado","50");
$produto2 = new Produto("Mouse","10");
$carrinho = new Carrinho();
$carrinho->add_produto($produto);
$carrinho->add_produto($produto2);
$carrinho->print();
?>
Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site
– Maniero