spence911 Posted November 18, 2013 Share Posted November 18, 2013 I'm a beginner currently studying Object Oriented Programming. The code in question is quite long so I will just share the snippet that I can't understand. private $productlist = array(); public function addproduct($product){ $this->productlist[] = $product; } public function stockup(){ foreach($this->productlist as $product){ $product->addstock(100); } I'm sure that function addproduct adds product objects to the product list array. What does the foreach loop: foreach($this->productlist as $product) in the stockup function do? Does it take those values and store them back in $product? Quote Link to comment https://forums.phpfreaks.com/topic/284020-foreach-loop-in-oop/ Share on other sites More sharing options...
Solution AbraCadaver Posted November 18, 2013 Solution Share Posted November 18, 2013 The foreach construct provides an easy way to iterate over arrays. http://us2.php.net/manual/en/control-structures.foreach.php It is iterating over each item in $this->productlist (which appear to be product objects) so that you can reference it as $product and do something with it. It appears that it is call the addstock() method on each object and adding 100 to the in stock quantity of each product, but we would need to see the addstock() function to tell. Quote Link to comment https://forums.phpfreaks.com/topic/284020-foreach-loop-in-oop/#findComment-1458825 Share on other sites More sharing options...
spence911 Posted November 18, 2013 Author Share Posted November 18, 2013 Crystal clear now. Thanks Cracka Memba. Quote Link to comment https://forums.phpfreaks.com/topic/284020-foreach-loop-in-oop/#findComment-1458858 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.