kansasakki Posted April 14, 2011 Share Posted April 14, 2011 hello all, I am new to php, and getting better using the object oriented approach. I have been a procedural programmer for several years, with a limited exposure to OOP. I have a question about pulling two values from an object. the following code is an excerpt from a class form. function getstocklist($currentcount){ $transactions = $currentcount['count']; $current = mysql_query("SELECT * FROM current_positions WHERE positiontype = 'long' OR positiontype = 'short'"); $tickers = ''; while ($stock = mysql_fetch_array($current)){ $tickers .= $stock['positionticker'] . ','; $stockList[] = array( 'shares' => round(($stock['positioncost']/$stock['positionprice']),0), 'date' => date("m/d/Y G:i:s", $stock['positiontime']-(45*60)), 'ticker' => $stock['positionticker'], 'type' => $stock['positiontype'], 'price' => $stock['positionprice'], ); } return($tickers); } I would like to get both the value of $tickers and the array $stockList. I am not sure how to proceed. any suggestions would be greatly appreciated. Thanks Kansas Quote Link to comment Share on other sites More sharing options...
gizmola Posted April 14, 2011 Share Posted April 14, 2011 I'm not sure what this has to do with Oop vs. procedural. The mechanics of "return" are the same. You can return a single variable, however that is not a limitation, as the variable can be any of the php types, including objects themselves. The simple answer here would be to return an array with both the items you need in it. return array('tickers' => $tickers, 'stocklist' => $stockList); Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted April 14, 2011 Share Posted April 14, 2011 You have a couple options: 1. Break this method into two methods - one for the ticker list, one for the more robust stock list information. 2. Return an array containing both the tickers and the fleshed out stock list. Quote Link to comment Share on other sites More sharing options...
kansasakki Posted April 14, 2011 Author Share Posted April 14, 2011 gizmola, thank you very much, this worked perfectly. Nightslyr: I was considering breaking it out into two different methods, was unsure of how to do it. return array worked as suggested. Thank you both very much! Kansas Quote Link to comment Share on other sites More sharing options...
kansasakki Posted April 14, 2011 Author Share Posted April 14, 2011 ... The mechanics of "return" are the same. You can return a single variable, however that is not a limitation, as the variable can be any of the php types, including objects themselves. The simple answer here would be to return an array with both the items you need in it. This was a lesson in itself. Quote Link to comment Share on other sites More sharing options...
maxudaskin Posted April 14, 2011 Share Posted April 14, 2011 I would highly suggest reading this tutorial. You may learn something, even though I had to take a class to completely understand the point of a class. http://www.phpfreaks.com/tutorial/oo-php-part-1-oop-in-full-effect Quote Link to comment Share on other sites More sharing options...
kansasakki Posted April 14, 2011 Author Share Posted April 14, 2011 I would highly suggest reading this tutorial. You may learn something, even though I had to take a class to completely understand the point of a class. http://www.phpfreaks.com/tutorial/oo-php-part-1-oop-in-full-effect Thank your for the tutorial, I have read it over and bookmarked the link! Kansas Quote Link to comment 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.