views:

70

answers:

3

This is a topic that, as a beginner to PHP and programming, sort of perplexes me. I'm building a stockmarket website and want users to add their own stocks. I can clearly see the benefit of having each stock be a class instance with all the methods of a class. What I am stumped on is the best way to give that instance a name when I instantiate it. If I have:

class Stock() {
   ....doing stuff.....

}

what is the best way to give my instances of it a name. Obviously I can write:

$newStock = new Stock();
$newStock.getPrice();

or whatever, but if a user adds a stock via the app, where can the name of that instance come from? I guess that there is little harm in always creating a new child with $newStock = new Stock() and then storing that to the DB which leads me to my next question!

What would be the best way to retrieve 20 user stocks(for example) into instances of class Stock()? Do I need to instantiate 20 new instances of class Stock() every time the user logs in or is there something I'm missing?

I hope someone answers this and more important hope a bunch of people answer this and it somehow helps someone else who is having a hard time wrapping their head around what probably leads to a really elegant solution. Thanks guys!

+1  A: 

I would suggest doing something like:

class Stock()
{
   public var $symbol;

   public function __construct($symbol)
   {
      $this->symbol = $symbol;
   }

   public function lookup()
   {
       // do something
       return $data;
   }
}


$stock = new Stock('AAPL');
$data[] = $stock->lookup();

So you could just $stock->symbol = 'NEWSYMBOL'; and then $stock->lookup();

Jud Stephenson
I understand the idea of instantiating the stock. The problem for me lies in the fact that if a user adds 3 new stocks, they'd all be instantiated with the variable $stock. That, or as soon as they're created they get put in the DB and then on retrieving them I need to NOT retrieve them into instances but just into a table or an array. That's my conundrum is that with this method you can only ever have one instance at a time because the only variable name is $stock. Does that make sense? Thanks for the response.
dscher
dscher, I guess I'm having a hard time understanding exactly what your saying, but $stock is one instance of the Stock class, although it can be reused for different ticker symbols.
Jud Stephenson
Jud, thanks fro the reply. Let's say that I have 30 stocks to load on a page into a table, should I use $stock over and over again in a loop to load each one and display it? Or would I NOT use the Stock Class and instead just query the DB and put the data into a table and then ONLY instantiate when someone requested a specific stock? Thanks a lot for the answer.
dscher
In the end, it is all the same. A class can contain db logic, it is just all modularized for reuse.
Jud Stephenson
I understand that. As mentioned below in another answer, what about creating an instance of Stock as an array and using stock tickers to represent individual stocks? Would that be a more elegant and functional solution in your view? Thanks for the input!
dscher
If you are going to have multiple instances of Stock, store them in an array. Talking about everything so abstractly makes it tough to nail down an elegant answer because there are so many solutions, depending on what your actually wanting to do.
Jud Stephenson
Yes, I understand. From what I can tell using an array is the cleanest, most orderly way to do this and gives me the tools to loop over the array without too many issues. Thanks, I think I've found my answer.
dscher
A: 

I don't understand why you wouldn't just make a Stock() object and have it load the various stock properties from a database. This seems a lot safer way to go and a lot less complicated to me.

niggles
I would do that if I understood the way to do it. Does that mean that when a user logs in all of their stocks are then instantiated as new objects? How are those new instances named? Or do you only instantiate them when someone chooses to edit them and Stock-based methods are needed?
dscher
You would set various properties e.gYou could write a function like:function findStock($symbol){ $db->get_row("SELECT * FROM `stock` WHERE `stock_symbol` = '" . mysql_real_escape_string($symbol) . "'"); etc.....}and use the results in a function which sets the details:function setStock($mySqlResults){ $this->name = $mySqlResults['stock_name']; $this->symbol = $mySqlResults['stock_symbol'];}-------or maybe I completely misunderstood the question?
niggles
A: 

You can just use an array of stocks. Note that [] means add a new item to the array.

$stocks[] = new Stock();

You can iterate through all of them like this:

foreach ($stocks as $stock) {
    echo $stock->lookup();
}

You can access a particular stock like this:

$stocks[7]->display();
Lotus Notes
So I could instantiate a new object into an array such as: $stocks['AAPL'] = new Stock();Is that correct? Therefore all stocks for a particular user could be kept in one instance of $stocks that is just an array? If so, I think you've answered my question in a way I didn't expect but quite like. Thanks!
dscher
Your answer wins, thanks for the easy explain.
dscher