5
I’m learning OO and venturing into PHP, only I came across something that I think in theory should work, but in practice does not work.
<?php
class Users{
public $name;
public $idade;
public $email;
private $senha;
function __construct($name, $idade, $email, $senha){
$this->name = (string) $name;
$this->idade = (int) $idade;
$this->email = (string) $email;
$this->senha = $this->setPassword($senha);
echo "O objeto foi contruido!";
}
function setPassword($senha){
if (strlen($senha) > 8 and strlen($senha) < 13):
$this->senha = password_hash($senha, PASSWORD_DEFAULT);
else:
die ('Sua senha deve conter entre 8 e 13 caracters');
endif;
}
}
There when I use :
$pessoa = new Users("Flavio", 19, "[email protected]", "testando123");
var_dump($pessoa);
He printa :
O objeto foi contruido!
C:\wamp\www\ws_php\n.php:6:
object(Users)[1]
public 'name' => string 'Flavio' (length=6)
public 'idade' => int 19
public 'email' => string '[email protected]' (length=22)
private 'senha' => null
the password becomes null.
but when I try :
$pessoa->setPassword("testando123");
it works normally.
Where am I going wrong?
One more question I have is about something I saw that’s called hinting type something like this I believe.
I’m saying here I want $nome
only accept the type string:
$this->name = (string) $name; // AQUI
$this->idade = (int) $idade;
$this->email = (string) $email;
$this->senha = $this->setPassword($senha);
but I saw that in PHP 7 it is possible to pass the function parameters.
function __construct(string $name, int $idade, string $email, $senha)
But when I do this does not work and me is returning an error on the console, I am doing something wrong?
Codestyle question: it is more common to use
{}
instead of:
in ifs and the like.– gmsantos