How to list the properties of an object in php using foreach?

4

I have the following class:

class Usuario{
     private $nome;
     private $profissao;

     function setNome($nome){
         $this->nome = $nome;
     }

     function getNome(){
         return $this->nome;
     }

     function setProfissao($profissao){
         $this->profissao = $profissao;
     }

     function getProfissao(){
         return $this->profissao;
     }
 }

Here I instantiate an object

$user = new Usuario();

$user->setNome('Nome Qualquer');
$user->setProfissao('Profissão Qualquer');

I wanted to know how I can list all the properties of this object using foreach . I already know there is get_object_vars that already does this for me. But I wanted to use the same foreach for this purpose. Of course the above example is just a simple example.

    
asked by anonymous 02.08.2016 / 16:26

3 answers

4

Would that help?

<?php
    class MyClass
    {
        public $var1 = 'value 1';
        public $var2 = 'value 2';
        public $var3 = 'value 3';

        protected $protected = 'protected var';
        private   $private   = 'private var';

        function iterateVisible() {
           echo "MyClass::iterateVisible:\n";
           foreach ($this as $key => $value) {
               print "$key => $value\n";
           }
        }
    }

    $class = new MyClass();

    foreach($class as $key => $value) {
        print "$key => $value\n";
    }
    echo "\n";


    $class->iterateVisible();

    ?>

Source: link

    
02.08.2016 / 19:22
4

You can create a method to list the attributes internally, nothing prevents you from using a foreach in this way:

class Usuario
{
     private $nome;
     private $profissao;

     public function setNome($nome)
     {
         $this->nome = $nome;
         return $this;
     }

     public function setProfissao($profissao)
     {
         $this->profissao = $profissao;
         return $this;
     }

     public function getNome($nome)
     {
         return $this->nome;
     }

     public function getProfissao($profissao)
     {
         return $this->profissao;
     }

     public function getValueByAttributeName($name)
     {

        if (property_exists($this, $name)) {
           return $this->$name;
        }
     }

    public function getValueByMethodName($name)
    {
        $name = $name.'()';

       if (method_exists($this, $name)) {

           return $this->$name;
       }
    }

    public function getAllAttributes()
    {
        $array = array();

        foreach ($this as $key => $value) {
            if (property_exists($this, $key)) {
                 $array[] = array($key => $value);
            }
         } 
         return $array;

    }
}

$usuario = new Usuario;

$usuario->setNome('Nome Qualquer');
$usuario->setProfissao('Profissão Qualquer');
$data = $usuario->getAllAttributes();

echo 'Exemplo de listar tudo: <pre>';
print_r($data);
echo '</pre><br>Exemplo por método:<br>';
echo '--->'.$usuario->getValueByMethodName('getnome');
echo '<br>';
echo '--->'.$usuario->getValueByMethodName('getprofissao');
echo '<br>Exemplo por atributo:<br>';
echo '--->'.$usuario->getValueByAttributeName('nome');
echo '<br>';
echo '--->'.$usuario->getValueByAttributeName('profissao');

IDEONE

    
02.08.2016 / 17:20
1

In the code you posted, the properties are set to private.

To list these properties, you would have to create a public method where you would then do the foreach and return the result.

Otherwise use Reflection or Closure Bind .

    
02.08.2016 / 16:31