2
I’m having some difficulties in instantiating objects in php.
I have the code in OO:
class Usuario {
private $idade;
private $nome;
public function getNome() {
return $this->nome;
}
public function setNome($nome) {
$this->nome=$nome;
}
public function getIdade() {
return $this->idade;
}
public function setIdade($idade) {
$this->idade=$idade;
}
}
And the test class:
include("Usuario.php");
classe TesteUsuario {
$usuario1 = TesteUsuario();
$usuario2 = New Usuario();
}
Instantiating the object within the scope of the class gives the error:
( ! ) Parse error: syntax error, Unexpected 'Testeusuario' (T_STRING) in C: wamp www lists Testeusuario.php on line 4 >
If instantiating outside also gives error.
How do I do it? I’ve already realized that I can use the test file without class, i.e.:
include("Usuario.php");
$usuario2 = New Usuario();
It is unlikely that I wanted to actually do this. Doing out works perfect if I use the correct syntax. There are several errors in this code. Doing inside the class is what has already been answered, but to create an instance of the class itself in its constructor does not give.
– Maniero
@bigown what are code errors? Grateful.
– André Nascimento