Classes and php variable scope

1

Well, my question is more about how to reference and "get" the contents of a variable in a PHP Class. See excerpts from the code below, where I put wrong data for the connection, on purpose, to cause an exception to the PDO, and thus store the error msg in the msgErro variable. Hi, I'm using XDebug to do this, but I'm not sure how to do this, but I'm not sure how to do this. when debugging progresses, and returns to PHP's register, which looks at the debugger, this shows the local variables, in this case the object that took the properties of the class, but the value of the property (variable) $ u- > msgErro is empty (emptied)! and this is what I can not understand how it is empty, if a line before, in the variables of the file Usuarios.php, where the class is, was content.

file register.php . .

    $u->conectar("xxxx","xxxx","xxx","");
    if ($u->msgErro == "") 

file usuarios.php // that contains the class

Class Usuario 
{
    private $pdo;
    public $msgErro; //tudo ok

    public function conectar($nome, $host, $usuario, $senha){
            global $pdo;
            try 
            {
                $pdo = new PDO("mysql:dbname=".$nome.";host=".$host,$usuario,$senha);
            }
            catch (PDOException $e)
            {
                $msgErro = $e->getmessage();
            }
    }
}

When I test the variable $ msgErro in the file register, through the object $ u = new User;

        if ($u->msgErro == "") 
    {
        echo "esta mensagem ´é se msgErro está vazia!";

That is, in this test, there should be a negative of if, because there was an error in the connection, the error was placed in the variable $ msgErro there in the file usuarios.php, however, in the test of the file cadastrar.php, the variable is empty, so go straight through if. Any tips? grateful.

    
asked by anonymous 08.09.2018 / 05:45

1 answer

2

If you want to refer to the member of the class, and not to any variable, you need to make explicit:

catch (PDOException $e)
{
    $this->msgErro = $e->getmessage();
}
//    ^
//    |
//     --------- O $this se refere à instância atual do objeto.
//               se fosse membro estático seria self::membro
//               (o self faz bypass na vtable do objeto,
//               definitivamente acessando a implementação original)

See giving error :) IDEONE , as expected.


Not that it is related to the original problem, but look at the "salad" of scopes you are doing with $pdo :

private $pdo;
public $msgErro; //tudo ok

public function conectar($nome, $host, $usuario, $senha){
   global $pdo;
   try 
   {
      $pdo 
   ...

You are not using $this , have a global left, and still a private.

If you want to make the OO "right", and need to reuse the PDO (it does not seem to be your case), inject the PDO in the constructor or via the method, otherwise it will subclass (I do not like it, but better than global), but do not rely on global ones. It makes perfect sense.

    
08.09.2018 / 06:44