RuleBritannia Posted January 7, 2014 Share Posted January 7, 2014 Hello If we pass a value to a object when creating it with __construct $NewObject = new class($value) Lets say our object is placed within a loop to do some further stuff while(1 < 10) { $NewObject->doThis(); $NewObject->doThat(); 1++; } Now, we want to pass the next value to the object, so it will doThis() and doThat in the loop, But currently, it will just perform doThis() and doThat() on the same value. How is this professionally achieved? We can destroy the object and put $NewObject = new class($value); inside the loop, But is there a better way? Thanks in advance for any future help/thoughts. Quote Link to comment https://forums.phpfreaks.com/topic/285165-passing-new-value-to-object-within-loop/ Share on other sites More sharing options...
RuleBritannia Posted January 7, 2014 Author Share Posted January 7, 2014 I think I had a brain messup when designing this weeks ago. I guess its "properly" done by passing the values in a method that's callable manually other that __construct. This way we don't have to destroy and start new objects. Sorry for the question, Unless I have still got it wrong, in which case the question still applies. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/285165-passing-new-value-to-object-within-loop/#findComment-1464244 Share on other sites More sharing options...
cyberRobot Posted January 7, 2014 Share Posted January 7, 2014 You could store the value as a class property: http://www.php.net/manual/en/language.oop5.properties.php Quote Link to comment https://forums.phpfreaks.com/topic/285165-passing-new-value-to-object-within-loop/#findComment-1464245 Share on other sites More sharing options...
RuleBritannia Posted January 7, 2014 Author Share Posted January 7, 2014 You could store the value as a class property: http://www.php.net/manual/en/language.oop5.properties.php Yes that would work, with the setter of that object not being in a __construct. With regards to this overall question, What about many properties that have been set later during the objects use? example var $date var $time Im guessing we should set all properties to null when we assign a new value to the "main" property? otherwise, we will be using older set values of properties set from a previous "main" value, (within the same new instance however) However if we just destroyed the object and started new, all of this would be done automatically, But I am wondering which is the professional way?? kill object, rebuild new or build once, reset all with new use(via method to set all var inside to null) Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/285165-passing-new-value-to-object-within-loop/#findComment-1464258 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.