Adding an external variable to the php class

Asked

Viewed 115 times

-2

Hello folks I am creating a class for user data and would like to know how to insert a sternal 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 name when I var_dump, gives error .

could help me?

  • The syntax seems to be correct. Why not edit the question and add the error message and the complete code?

  • Notice: Undefined variable: nome_completo in (camiododiretorio)

  • Full code now...

  • Actually it is just a test, this variable $full name assign my name on it test "Malone Castro" and gave var_dump and gave error

  • 1

    Even if it is a test, the code needs to be complete. If you have not set the variable, it will obviously result in this error.

  • If I will use for various users I can not assign a value only right friend? rs

  • the variable set within the class? would this be?

  • and how to do this using PDO

  • 1

    If you are going to assign several, it depends on how this data will arrive. How is the database? How do you recover this data? How are you iterating on them? If you don’t ask a clear question, you can’t get a clear answer.

  • Data recovers via SELECT in DB using PDO class

Show 5 more comments

1 answer

1


The suggestion I give you is to use the getters and setters

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

class.usuario.php:

class usuario {
  private $nome;

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

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

In your class.user.php retrieving the data from the database, don’t forget to use require or include;

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

And where will visualize the variable:

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

I hope it helps.

Hugs, Hit!

  • thanks friend, I’m enjoying using the __Construct function and you love to use it?

  • Yeah. I think it’s cool!

  • blza brother!!! you are the guy You have Zap?

  • Yes, Mr Rsrs.

  • passes there, has many friends adventists rs

  • e-mail to [email protected], which step blz?

  • the IDE itself generates so when requested

  • 1

    @Papacharlie yeah, I commented and I didn’t realize it -.-.

Show 3 more comments

Browser other questions tagged

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