Skip to content
Advertisement

How can I remove a private property from an array of object?

$array = $this->em->getRepository($entity)->filter($id);

With doctrine I am creating an array:

array:24 [▼
  0 => Members^ {#543 ▼
    -id: 1
    -username: "lio"
    -email: "info@somepage.com"
    -isActive: true
    -name: "Lio"
    -projects: PersistentCollection^ {#590 ▶}
    -pages: PersistentCollection^ {#615 ▶}
  }
  1 => Members^ {#135029 ▶}
  2 => Members^ {#125937 ▶}
  3 => Members^ {#1807 ▶}
  4 => Members^ {#135075 ▶}
  5 => Members^ {#135086 ▶}

From this array I try to remove the object projects:

 foreach ($array as $value) {
      dump($value->projects);
    }

I get the error message that the object is private.

I found this post, but there it is written, that I need to write a function inside the class.

Removing private properties of object

My question is, is it possible to remove it outside the class? Because when I remove it inside the class it is always removed and I want to remove it only in a specific case.

Advertisement

Answer

This is an array of Member object. A private attribute of an object can only be access through its method. You need to find the file that declares the class Member. Then add a public class method to do the unset. For example,

class Member {

  // ...
  public function unsetProjects()
  {
    unset($this->projects);
  }

}

Then you should be able to do this:

foreach ($array as $value) {
  $value->unsetProjects();
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement