No return function

Asked

Viewed 146 times

0

I am following an OO study book and I came across a problem related to a method call. I will contextualize with the Classes.

<?php

    class Orcamento{

        private $itens;

        public function adicionar(Produto $produto, $qtde){
            $this->itens[] = array($qtde, $produto);
        }

        public function calculaTotal(){
            $total = 0;

            foreach ($this->itens as $item) {
                // echo "<pre>";print_r($item[0]);die();
                echo "<pre>";print_r($item[1]);die();
                // echo "<pre>";var_dump($item[1]->getPreco());die();
                $total +=($item[0] * $item[1]->getPreco());
            }
                // die($total);
            return $total;
        }

    }

?>

<?php

    class Produto{
        private $descricao;
        private $estoque;
        private $preco;

        public function __construct($descricao, $estoque, $preco){
            $this->$descricao = $descricao;
            $this->$estoque = $estoque;
            $this->$preco = $preco;
        }

        public function getPreco(){
            return $this->preco;
        }

    }


?>

Cria Orçamento

    $orcamento = new Orcamento();

    $orcamento->adicionar( new Produto('Maquina de café', 10, 299), 1);

    $orcamento->adicionar( new Produto('Mesa de vido', 10, 170), 1);

    $orcamento->adicionar( new Produto('Barra de chocolate', 10, 7), 3);

    // echo "<pre>";print_r($orcamento);die();

    print $orcamento->calculaTotal();

?>

It is an example using aggregation the problem is that whenever the getPreco method is called the same does not return anything. The strangest thing about executing the die is that the Object has all the right Attributes (quantity and price). So much so that when running the die on line 16 of the file Orcamento.php the return is this :

Produto Object
(
    [descricao:Produto:private] => 
    [estoque:Produto:private] => 
    [preco:Produto:private] => 
    [Maquina de café] => Maquina de café
    [10] => 10
    [299] => 299
)

Now I don’t know if it’s a syntax error or a structure error.

  • 2

    $this->$preco = $preco, there is no this $ before price, it should be $this->preco = $preco

  • just as @Andersoncarloswoss said, you have the same reptindo error here $this->$descricao = $descricao; $this->$estoque = $estoque; $this->$preco = $preco;, after the -> doesn’t have the $, that explains why these values are empty

2 answers

1

You have created an array.

So when going through the array items[] stay tuned in the following information

Array[0] =>
     [ '0' => quantidade, '1' => Produt1],
Array[1] =>
     ['1' => quantidade, '1' => Produt2],
Array[2] =>
     ['0' => quantidade, '1' => Produt3],
Array[3] =>
     ['1' => quantidade, '1' => Produt4],
Array[4] =>
     ['1' => quantidade, '1' => Produt5],
Array[5] =>
     ['0' => quantidade, '1' => Produt3],
Array[6] =>
     ['0' => quantidade, '1' => Produt4],
Array[7] =>
     ['0' => quantidade, '1' => Produt5],

Realize that you have to go through two vectors The first is to position the item and the second extract quantity and the instantiated product.

Use var_dump($vector or Object); With var_dump you see how the data is coming out in more detail. But error is pq not implemented using this vector traverse logic.

1


I believe the problem is:

    public function __construct($descricao, $estoque, $preco){
        $this->$descricao = $descricao;// aqui 
        $this->$estoque = $estoque;//     aqui
        $this->$preco = $preco;//         e aqui
    }

'Cause you’re putting the $ to define an object element, after $this-> on the three lines, it should be like this:

    public function __construct($descricao, $estoque, $preco){
        $this->descricao = $descricao;
        $this->estoque = $estoque;
        $this->preco = $preco;
    }

You can check that the values that do not appear are:

[descricao:Produto:private] => 
[estoque:Produto:private] => 
[preco:Produto:private] => 

Which correspond to these three errors respectively.

  • 1

    That was exactly the problem , including the calculation worked. Thanks for the touch regarding the errors I thought was the way PHP present privates attributes.

Browser other questions tagged

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