chupinette Posted December 30, 2009 Share Posted December 30, 2009 Hello all I wanted to know of it is possible to create instances of two different classes in a single php page? Im trying to display a list of categories and another list of brands. But I am getting only the categories as result... this is what i have done: <?php $obj = new CategoryList();if (method_exists($obj, 'init')){ $obj->init(); }for($i = 0;$i< count($obj->mCategory); $i++) {echo "<a href=''>"; echo $obj->mCategory[$i]['name']. "<br/>"; echo "</a>"; } $obj2 = new BrandList();if (method_exists($obj2, 'init')){$obj2->init(); }for($i = 0;$i< count($obj2->mBrand); $i++){echo "<a href=''>"; echo $obj2->mBrand[$i]['name']. "<br/>"; echo "</a>"; } ?><?php $obj = new CategoryList(); if (method_exists($obj, 'init')) { $obj->init(); } for($i = 0;$i< count($obj->mCategory); $i++) { echo "<a href=''>"; echo $obj->mCategory[$i]['name']. "<br/>"; echo "</a>"; } $obj2 = new BrandList(); if (method_exists($obj2, 'init')) { $obj2->init(); } for($i = 0;$i< count($obj2->mBrand); $i++) { echo "<a href=''>"; echo $obj2->mBrand[$i]['name']. "<br/>"; echo "</a>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/186684-instance-of-two-classes/ Share on other sites More sharing options...
Daniel0 Posted December 30, 2009 Share Posted December 30, 2009 You can create as many objects as you want as long as you have sufficient memory. By the way: http://en.wikipedia.org/wiki/Indent_style Pick one and use it. Quote Link to comment https://forums.phpfreaks.com/topic/186684-instance-of-two-classes/#findComment-985919 Share on other sites More sharing options...
chupinette Posted December 30, 2009 Author Share Posted December 30, 2009 Thanks for your reply! I think Ive put the code twice.. Can you please tell me why only the categories are being displayed? Here's the code again: <?php $obj = new CategoryList(); if (method_exists($obj, 'init')) { $obj->init(); } for ($i = 0; $i < count($obj->mCategory); $i++) { echo "<a href=''>"; echo $obj->mCategory[$i]['name'] . "<br/>"; echo "</a>"; } $obj2 = new BrandList(); if (method_exists($obj2, 'init')) { $obj2->init(); } for ($i = 0;$i < count($obj2->mBrand); $i++) { echo "<a href=''>"; echo $obj2->mBrand[$i]['name'] . "<br/>"; echo "</a>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/186684-instance-of-two-classes/#findComment-985928 Share on other sites More sharing options...
Daniel0 Posted December 30, 2009 Share Posted December 30, 2009 My guess would be that count($obj2->mBrand) evaluates to 0 meaning the last for loop doesn't run. Quote Link to comment https://forums.phpfreaks.com/topic/186684-instance-of-two-classes/#findComment-985930 Share on other sites More sharing options...
chupinette Posted December 30, 2009 Author Share Posted December 30, 2009 When I comment the part for the categories, it actually displays the brands. That is why I was wondering if it is possible to create instances of two different classes in a single php file? Quote Link to comment https://forums.phpfreaks.com/topic/186684-instance-of-two-classes/#findComment-985933 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.