OOP - Handling Automated Instances of a Class - PHP
Posted
by dscher
on Stack Overflow
See other posts from Stack Overflow
or by dscher
Published on 2010-03-26T02:17:59Z
Indexed on
2010/03/26
2:23 UTC
Read the original article
Hit count: 289
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!
© Stack Overflow or respective owner