I have a requirement to accept an array of checked items from a table and update a field based on which items have been selected. Initially my thoughts where to simply loop over each of these items in the array and access a function in the specific class to update the status.
开发者_StackOverflow社区I'm slightly concerned about this approach as it would mean instantiating an object for each iteration of the loop in order to update the relevant status.
foreach($example as $exampleId){
$newExample=new Example($exampleId);
$newExample->updateStatus('active');
}
Are there any better ways around this? This seems like bad practice but I'm struggling to find an alternative way.
is this an option?
$newExample=new Example();
foreach($example as $exampleId){
$newExample->updateStatus($exampleId,'active');
}
otherwise you could always do this:
foreach($example as $exampleId){
$newExample=new Example($exampleId);
$newExample->updateStatus('active');
$newExample->__destruct();
unset($newExample);
}
for this you would need anothe method in your class
$newExample=new Example();
foreach($example as $exampleId){
$newExample->set_example_id($exampleId);
$newExample->updateStatus('active');
}
It sounds like creating your object has overhead because it is loading from a database or somewhere? Could you add a static method to Example
which updates without having to create an object which loads and populates itself? Then you could do:
foreach($example as $exampleId){
Example::UpdateExampleStatus($exampleId,'active');
}
精彩评论