How to add an external variable in class php

0
Hello everyone, I am creating a class for user data and I would like to know how to insert an external variable containing data, for example from the database:

 class Usuario {

     public $nomecompleto;
}

$DadosUsuaio = new Usuario;
$DadosUsuaio->nomecompleto = $nome_completo;

For example this variable $ full_name, name my case when I give var_dump, gives error.

Could you help me?

    
asked by anonymous 18.06.2017 / 20:51

1 answer

1

The suggestion I give you is to use getters and setters

Since you're using Object Orientation, it's easier:

  

class.user.php:

class usuario {
  private $nome;

  public function getNome(){
    return $this->nome;
  }

  public function setNome($nome){
    $this->nome = $nome;
    return $this;
  }
}

In your class.usuariodao.php retrieving the data that comes from the bank, not forgetting to use require or include;

 $dadosUsuario = new usuario();
 $dadosUsuario->setNome($row['nome']);
  

And where will you view the variable:

 $dadosUsuario = new usuario();
 echo $dadosUsuario->getNome();

I hope it helps.

Hugs, Success!

    
18.06.2017 / 22:04