How do I get a die from a class that was set in another class?

Asked

Viewed 63 times

0

I have a question in my application. I would like to retrieve data from one class that has been set in another. However when I do data recovery it turns null.

Here is my object

class geraHash {
private $codigo;

//Aqui é o encapsulamento dos dados
public function setCodigo($codigo){
   $this -> codigo = $codigo;
}
public function getCodigo(){
   return $this->codigo;
}

//Aqui é o método que gera o código de verificação
public function GeraCodigo($qtd){ 

    $Caracteres = 'ABCDEFGHIJKLMOPQRSTUVXWYZ0123456789'; 
    $QuantidadeCaracteres = strlen($Caracteres); 
    $QuantidadeCaracteres--; 

    $Hash=NULL; 
    for($x=1; $x <= $qtd; $x++){ 
        $Posicao = rand(0,$QuantidadeCaracteres); 
        $Hash .= substr($Caracteres,$Posicao,1); 
    } 

    return $Hash; 
}

//Aqui é o método que faz a verificacao e onde eu gostaria de obter o dado setado
public function VerificaCod($cod) {
    $codVerificacao = $this->getCodigo();
    //if ($cod === $codVerificacao){
        echo $codVerificacao, $cod;
        return TRUE;
    //}

}

}

There is another class where I send to the email and I select the data in my object.

<?php
 include 'geraHash.php';

 class loginDAO {

 public function recuperaSenha(loginVO $login){

    $mysqli = new mysqli("localhost", "root", "", "bdMeuBanco");
    $stmt = $mysqli ->prepare("SELECT emailLogin FROM tblLogin WHERE emailLogin = ?");
    $stmt ->bind_param("s", $p1);

    $p1 = $login ->getEmailLogin();

    $stmt ->execute();

    if($stmt ->num_rows >=0){
        $geraHash = new geraHash(); //Aqui ocorre a instancia de um novo objeto
        $cod = $geraHash ->GeraCodigo(6); // Aqui eu chamo o método que gera o cod que vai ser passado na msgm via email
        $geraHash->setCodigo($cod); //Aqui eu seto o dado no objeto



        $arquivo = "<html><head><meta charset='utf-8'></head><style type='text/css'> body{font-family: monospace;color: darkgray;font-size: 16px;width: 500px;}h3{color: black;}</style><body>Enviamos esse código para que possa redefinir sua senha.<br><h3>$cod</h3> <br>Se você não solicitou a redefinição de sua senha ignore este email!</body></html>";
        $destino = $login->getEmailLogin();
        $assunto = "Redefinição de senha";

        require_once('class.phpmailer.php'); //chama a classe de onde você a colocou.

        $mail = new PHPMailer(); // instancia a classe PHPMailer

        $mail->IsSMTP();

        //configuração do gmail
        $mail->Port = '465'; //porta usada pelo gmail.
        $mail->Host = 'smtp.gmail.com'; 
        $mail->IsHTML(true); 
        $mail->Mailer = 'smtp'; 
        $mail->SMTPSecure = 'ssl';

        //configuração do usuário do gmail
        $mail->SMTPAuth = true; 
        $mail->Username = '[email protected]'; // usuario gmail.   
        $mail->Password = 'password'; // senha do email.

        $mail->SingleTo = true; 

        // configuração do email a ver enviado.
        $mail->From = "[email protected]"; 
        $mail->FromName = "meu dominio"; 

        $mail->addAddress($destino); // email do destinatario.

        $mail->Subject = $assunto; 
        $mail->Body = $arquivo;
        $mail ->CharSet = "UTF-8";


        if(!$mail->Send()){
            echo "Erro ao enviar Email:" . $mail->ErrorInfo;
        }

        return TRUE;
    }

}

}

And my last file receives the data that was passed via email to make the check

<?php
include 'loginDAO.php';

$loginDAO = new loginDAO();

if(NULL !== filter_input(INPUT_POST, "btnConfirmar2")){ //Aqui verifica se o botão existe para receber uma acao

    $geraHash = new geraHash();
    $cod = filter_input(INPUT_POST, "userCodPass");
    if($geraHash ->VerificaCod($cod)){
        echo "Funcionou";
    }
}

I have come to the conclusion that the object is not receiving the value because I am instantiating the object twice in different classes. For that I would need to reuse the first instance, someone knows how I can do it?

p.s: I have used global but I didn’t get a result.

  • The problem is in calling VerificaCod()?

  • No, it’s the return of the die -> $codVerificacao = $this->getCodigo();

  • He returns me null

  • I suspect that the beginning of the problem is the last bit of code. In it should have a call setCodigo()

  • the setCodigo() is in the file loginDAO

  • See the result of var_dump($geraHash->getCodigo()) before if($geraHash ->VerificaCod($cod)){

  • I did what he said, he called me back null :(

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.