0

There was a need to create a class object in the cycle. The cycle can be 1000 or more iterations. I'm having doubts about the use of memory. Therefore, the question of whether to do so?. Ready to hear the pros and cons of using this design. In advance thank everyone for answers.

Code example:

foreach($entities as $entity) {
    $object = new $class($entity);
    $object->doSomething();
}

2 Answers 2

2

Unless your class produce some trash in global space it won't affect (much) overall memory usage. Each instance will be disposed right at the end of cycle.

Sign up to request clarification or add additional context in comments.

Comments

1

This wont affect memory. After each loop the new $object will fall out of scope and will be eligible for garbage collection. PHP is then free to remove the object from memory.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.