-2
Good morning,
In the past articles I wrote I was mentioning that I was learning Object Orientation in PHP in the documentation itself, and now I decided to do some tests that maybe can be applied in real life, but there was no success.
I created a class in PHP called Person and an html form, but when I call the display method it cannot rescue the data that was passed.
class Pessoa {
public $nome;
public $idade;
public $sexo;
public $qualidade;
public $defeito;
public function __construct() {
$this->nome = $nome;
$this->idade = $idade;
$this->sexo = $sexo;
$this->qualidade = $qualidade;
$this->defeito = $defeitos;
}
public function apresentacao() {
echo("Olá! Meu nome é! $this->nome, eu possuo $this->idade anos.<br/>Sou do Sexo: $this->sexo");
}
}
//Aqui é feito um teste para verificar se estava sendo pego realmente, está tudo ok!
echo $nome = $_POST['nome'];
echo $idade = $_POST['idade'];
echo $sexo = $_POST['sexo'];
echo $qualidade = $_POST['qualidade'];
echo $defeito = $_POST['defeito'];
$pessoa1 = new Pessoa();
$pessoa1->apresentacao();
HTML form
<form method="POST" action="Animal.php">
<input type="text" class="" id="" name="nome">
<input type="text" class="" id="" name="idade">
<input type="text" class="" id="" name="sexo">
<input type="text" class="" id="" name="qualidade">
<input type="text" class="" id="" name="defeito">
<button type="submit">Enviar</button>
</form>
Edit1: My intention is not to apply any advanced standard (file organization type, separate classes, etc.) I would just like to see where I am missing, my goal is to understand what is missing, what I am doing and what was in excess.
Edit2: The error you are giving in my PHP at show time is: that the variables are not defined.
Edit3: It was not for when I call the method apresentar()
he already took and organized?
Notice: Undefined variable: in line name 15
Notice: Undefined variable: online age 16
Notice: Undefined variable: sex in line 17
Notice: Undefined variable: quality in line 18
Notice: Undefined variable: defects in line 19
echo $nome = $_POST['nome']
, what this line should do?– Woss
Remembering that this is just a test (the entire file), but this line would indicate to check if you were really taking the form data.
– Devprogramacao
Mainly because it is a test that the code should represent the reality of the question. You made a
echo
and a variable assignment on the same line. Did that make sense to you? What would be the expected result in this line, that is, what should be displayed byecho
and what the value of$nome
after running the line?– Woss
It really didn’t make much sense, thank you for the remark. To answer your question, it would be the form data, perhaps I did not understand very well.
– Devprogramacao