Variable with null value when calling the model method

0

I'm working with codeigniter . When submitting a form I am sending the data to model through getters and setters.

This is my input

div class="col-md-6">
                <div class="form-group">
                    <?php echo form_label('Fornecedores (CNPJ - Razão Social):*', 'fornecedor'); ?>
                    <?php echo form_dropdown('fornecedor', $fornecedores, set_value('fornecedor'), array("class" => 'form-control', 'required' => 'required'))?>
                </div>
            </div>

When you click submit, enter a method in the controller that has this code:

 $this->load->model('Movimento_model', 'movimento');
 $this->movimento->setCnpj($this->input->post('fornecedor'));

 echo $this->movimento->getCnpj(); // exibe para teste

This is my model (summarized):

class Movimento_model extends CI_Model {

 private $cnpj;

 public function setCnpj($cnpj)
{
    $this->cnpj = strtoupper(trim($cnpj));
}

 public function getCnpj()
{
    return $this->$cnpj; // LINHA DO ERRO
}

This is the error:

  

Undefined variable: cnpj

Note: I'm setting but it seems that it is not accepting. When I try to show the error occurs.

    
asked by anonymous 13.08.2018 / 22:17

1 answer

-1

Face tries an initial value for the variable

class Movimento_model extends CI_Model {

private $cnpj;
$this->cnpj = 0;

public function setCnpj($cnpj)
{
    $this->cnpj = strtoupper(trim($cnpj));
}

public function getCnpj()
{
    return $this->$cnpj; // LINHA DO ERRO
}
    
14.08.2018 / 01:12