1
$EntityName = 'App\\Entity\\' . ucwords($slug);
$item = new $EntityName();    
$item= $this->getDoctrine()->getRepository($EntityName)->find($id);
    $form = $this->createFormBuilder($item)

content of $item:

object(App\Entity\Members)#4788 (6) {
  ["id":"App\Entity\Members":private]=>
  int(9)
  ["username":"App\Entity\Members":private]=>
  string(13) "123"
  ["plainPassword":"App\Entity\Members":private]=>
  NULL
  ["password":"App\Entity\Members":private]=>
  string(0) ""
  ["email":"App\Entity\Members":private]=>
  string(7) "1@12.sw"
  ["isActive":"App\Entity\Members":private]=>
  bool(true)
}

I try to get the fields from $item.

As an output I want to have

id
username
password
email
isActive

to create the fields.

My approach is:

foreach($item as $field){
   echo $field;
}

But I do not get any output

peace_love
  • 6,229
  • 11
  • 69
  • 157
  • Usually that's done by using getters – revengeance Aug 29 '18 at 17:46
  • @revengeance The problem is that I need to get just every field that is in the object, with getters I only know to get a specific field if you already know the name of the field, but what if you do not know what is inside the object and want to get a kind of list of all fields inside – peace_love Aug 29 '18 at 17:49
  • Got you. I would write proper answer, but I'm from phone now. Check http://php.net/manual/en/function.get-object-vars.php There is answer. Basically you can cast to array or use get object vars function (from inside the class to get private variables) – revengeance Aug 29 '18 at 18:14

2 Answers2

1

You cannot loop over the properties of an object like that. If you want to do this in a loop you would have to provide an array of the properties.

foreach(['id', 'username', 'email'] as $field){
   echo $item->{$field}
}

If you just wanted to print one property you would do it like this: $item->username

Chip Dean
  • 4,222
  • 3
  • 24
  • 36
  • My bad since the properties are private it will not be available to `get_object_vars`. I'll update my answer. – Chip Dean Aug 29 '18 at 17:37
  • I cannot do it like this, because this information `'id', 'username', 'email'` is actually what I need as an output – peace_love Aug 30 '18 at 07:39
1

You can get a list of object properties with get_object_vars, and from each of these you can get to the value of each property using the magic method __get(). See this answer. EDIT: You actually don't even need the magic method... the get_object_vars returns values already.

Example modified from the above links:

class Foo {
    private $a;
    private $d;

    public function getAllFields() {
        return get_object_vars($this);
    }
}

Then you can get all (non-static) properties, including private properties, with this:

$item = new Foo();
$fields = $item->getAllFields();

and you can get values of each field like so:

foreach ($fields as $fieldName => $fieldValue) {
    echo $fieldName . ' has value ' . $fieldValue;
}

This is untested code, but should work.

ehymel
  • 1,360
  • 2
  • 15
  • 24
  • Sure it can, just place `get_object_vars` within the class code, literally straight from the [docs](http://php.net/manual/en/function.get-object-vars.php). I'll update my answer to show. – ehymel Aug 30 '18 at 13:20