1
You see, my question is more about how to reference and "get" the content of a variable in a PHP Class. See excerpts of the code below, where I put wrong data for the connection, on purpose, to cause an exception of PDO, and so store the error msg in the msgErro variable. So far, so good, only that debugging the code (with Xdebug), I see that in the file that has the class and the msgErro variable, it is created in a good way, I visualize its content (which is the connection error), however, when the debugging advances, and goes back to the PHP of the register, I look at the debugger, this shows the local variables, in the case the object that took the class properties, but the property value (variable) $u->msgErro is empty (empty)! and that’s what I can’t figure out how it gets empty, if a line earlier in the variables in the.php file, where the class is, was with content.
php. . .
$u->conectar("xxxx","xxxx","xxx","");
if ($u->msgErro == "")
user file.php // containing 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 $msgErro variable 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 error in the connection, the error was placed in the variable $msgErro there in the file users.php, however, in the test of the file register.php, the variable is empty, so it goes straight through if. Any tips? Grateful.
The best tip is to stay smart, if you really dedicate yourself to learning how to program, you will notice that almost every "teacher" of PHP does not understand any programming crap, and that almost always, using OO in PHP can be considered ignorance. I know if you ask around they’ll say it’s absurd, but it’s that thing, it depends on how much you really want to be a programmer. If you have time, learn how to program with C, C++, C#, and even Java. Then it will be easier to really learn, because in these more serious languages people can’t pretend to be programming.
– Bacco