Huligan
Huligan

Reputation: 429

How to save state of instance class?

There is one class:

Class A {
  public function Q(){
      return value;
  }
}

In loop I create some exemplars of class A:

while ($i < 5){
   $obj_$i = new A();
   $obj_$i->Q();
}

Where can I save all results of $obj_$i->Q(); for each iteration on loop?

Because I after I need to trasfer these objects in another class to handling.

Class B {
   public function Result(){
      return $obj_$i + 3;
   }

}

Upvotes: 1

Views: 125

Answers (1)

Rajdeep Paul
Rajdeep Paul

Reputation: 16963

Create an array and push the objects to this array in each iteration of while() loop, like this:

$objArray = array();
while ($i < 5){
   $objArray[] = new A();
}

Later, pass this array to Result() method of class B for further processing,

class B {
    public function Result($objArray){
        foreach($objArray as $obj){
            // your code
        }
    }
}

Upvotes: 1

Related Questions