Generating an array properly within a class method

Asked

Viewed 26 times

0

I have a class Products.

Within it all attributes are private.

Inside it I have the function below:

  public function getArray($cada) {

       $produto = array(
           'idProduto' => $cada->getIdProduto(),
           'tipo' => $cada->getTipo(),
           'modelo' => $cada->getModelo(),
           'bandejas' => $cada->getBandejas(),
           'peso' => $cada->getPeso(),
           'prensagem' => $cada->getPrensagem(),
           'precoUnitario' => $cada->getPrecoUnitario(),
           'comprimento' => $cada->getComprimento(),
           'largura' => $cada->getLargura(),
           'cabo' => $cada->getCabo(),
           'ligacao' => $cada->getLigacao(),
           'potencia' => $cada->getPotencia(),
           'cosumo' => $cada->getConsumo(),
           'corrente' => $cada->getCorrente(),
           'disjuntor' => $cada->getDisjuntor(),
           'descricao' => $cada->getDescricao(),
           'estoque' => $cada->getEstoque(),
           'freteGratis' => $cada->getFreteGratis(),
           'bloqueado' => $cada->getBloqueado()
           );

       return $produto;

  }  

It turns out that the

return $produto

is giving

Notice: Undefined variable: produto in D:\Trabalhos\host\htdocs\hotplateprensas.com.br\_controlls\_models\Produtos.php on line 171

Line 171

return $produto;

It generates this error and then generates the empty array. I tried to create an attribute and make it the way below>

$this->produto = array(
...
);
 return $this->produto;

But then generates the empty array but the error disappears.

What is wrong?

  • Apparently within one of the methods you are not instantiating the product variable, what is the product class structure?

  • It’s just a local variable. Not an attribute, so instantiation is not like I did, within the method?

  • Make all the mistake!

  • I changed at the end of the question to put the complete error

No answers

Browser other questions tagged

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