I'm learning OO and venturing into PHP, but I've come across something I believe in theory should work, but in practice it does not.
<?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;
}
}
Then 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 is called type hinting something so I believe.
I'm saying here that I want to $nome
only accept the string type:
$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 in the parameters of the function.
function __construct(string $name, int $idade, string $email, $senha)
But when I do this it does not work and it is returning a bug in the console, am I doing something wrong?