php – Variable is not updated in constructor

Question:

I'm learning OO and venturing into PHP, but I've come across something that I think in theory should work, but in practice it doesn't.

<?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, "flvdeveloper@gmail.com", "testando123");
var_dump($pessoa);

He prints:

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 'flvdeveloper@gmail.com' (length=22)
  private 'senha' => null

the password is 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 like that I believe.

I'm saying here that I want $nome only accept 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 function parameters.

function __construct(string $name, int $idade, string $email, $senha)

But when I do this it doesn't work and I am returning an error on the console, am I doing something wrong?

Answer:

It's a very simple mistake, but at the same time, annoying to notice, and it's here

$this->senha = $this->setPassword($senha);

You are calling the method

$this->senha = $this->setPassword($senha);
               ^^^^^^^^^^^^^^^^^^^^^^^^^^

And this method does not return anything (null).

But you take this null and store it in $this->senha right away:

$this->senha =  $this->setPassword($senha);
^^^^^^^^^^^^^^

That is, you have just overwritten what setPassword created.

The correct thing would be to unassign, and let the method work alone:

function __construct($name, $idade, $email, $senha){
    $this->name = (string) $name;
    $this->idade = (int) $idade;
    $this->email = (string) $email;
    $this->setPassword($senha);
    echo "O objeto foi contruido!";
}

See working on IDEONE .

About Type Hinting , as you used the tag, it's worth saying that they are now Type Declarations , but unfortunately they are not so suitable to help you, as they work a little bit specifically.

When you declare in the function, for example a bool , the function will expect an "instance of bool", not the primitive type.

More details in the manual:

http://php.net/manual/en_BR/functions.arguments.php#functions.arguments.type-declaration

Another thing: $a = ( tipo ) $b; are casts , not related to Type Declaration or Hinting . In your case, they won't prevent the person from using wrong values.

More details in the manual:

http://php.net/manual/pt_BR/language.types.type-juggling.php

Finally, instead of die(); would suggest you create a flag in your code to say if the object is valid or not.

Something like:

class Users{
  public $name; 
  public $idade;
  public $email;
  public $isValid;
  private $senha;

  function __construct($name, $idade, $email, $senha){
    $this->name = $name;
    $this->idade = $idade;
    $this->email = $email;
    $this->isValid = $this->setPassword($senha);
  }

  function setPassword($senha){
    if (strlen($senha) > 8 and strlen($senha) < 13) {
      $this->senha = password_hash($senha, PASSWORD_DEFAULT);
      return true;
  } else {
      $this->senha = '';
      return false;
  }

  function isValid(){
    return $this->isValid;
  }

How to use:

$pessoa = new Users( 'Flavio', 19, 'flvdeveloper@gmail.com', 'testando123' );
if( $pessoa->isValid() ) {
    // faz o que tem que fazer
else {
    // avisa que deu problema
}
Scroll to Top