Doubt about attributes and POO methods in PHP

Asked

Viewed 82 times

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;
    }
}

2 answers

4

Simply the "print" of the last part you mentioned, is a method of Usuario.

Note on this line:

public function cadastrar(array $produtos, Usuario $usuario)
                                           ^^^^^^^

The "register" method waits for a object user-type.

And yet, within the method, this object is stored in the class member:

$this->usuario = $usuario;


What you were in doubt is mere unfolding:

$r .= "Usuario" . $this->usuario->imprimir(); 
//                                   ^--- método do Usuario cadastrado
//                          ^--- objeto armazenado previamente no cadastrar

Here is called the method imprimir(); class Usuario.

The fact of having a method imprimir() in Compra is "practically" a coincidence.

It would be the case that you take a peek at the implementation of the class Usuario to confer.

Everything here applies to Produtos also, but I imagine that at these times of the championship you have already understood. It is worth noting that despite produtos was array, everything indicates that it is a array of objects of the type Produto, which is dismembered in the foreach:

foreach ($this->produtos as $produto) {...
  • 1

    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!

2


From what I understand, the Shopping class expects an object from the User class, which must have a print method, and a collection of objects from the Product class, which must also have a print method.

<?php
class Produto
{
    public $id;

    public function imprimir()
    {
            $r = 'Eu sou o produto #' . $this->id;

            return $r;
    }
}

class Usuario
{
    public $id;

    public function imprimir()
    {
            $r = 'Eu sou o usuário #' . $this->id;

            return $r;
    }
}
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.