How do I compare the property of an object with a string? [closed]

1
class Alunos{

   public $nome; 

}

$aluno= new Alunos();

$aluno->nome="Pedro";

if(propriedade=="nome")

{

   echo $aluno->nome;

}
    
asked by anonymous 06.06.2017 / 04:50

1 answer

0

I think what you need is the property_exists function. It returns true if a class or object has a certain property and false otherwise.

class Alunos{

   public $nome; 

}

$aluno= new Alunos();

$aluno->nome="Pedro";

if (property_exists("Alunos", "nome"))
{
   echo $aluno->nome, PHP_EOL; // Pedro
}

Or by using the object itself:

if (property_exists($aluno, "nome"))
{
   echo $aluno->nome, PHP_EOL; // Pedro
}
    
06.06.2017 / 14:22