I'm starting to moving into a more object oriented approach when creating my PHP scripts.
However, there is a scenario that probably is quite common, but I don't know the best way to solve: Finding a specific element in an array. (Before OOP I didn't need to worry since I could find it with SQL in the database.)
Let's say I have a supermarket with different products. I would create a Supermarket object, and add the products from the database as an array in the supermarket:
Now I have an array of products that can be looped through with foreach($market->products AS $product). That's all good.
But how do I access a specific product? Let's say I want to change the price for the product with ID = 40. How do I access this product (to call its "setPrice()" function)?
I can see two solutions:
1. I implement a "getProduct($id)" function in the supermarket that loops through all products and returns the one with the correct $id. Seems inefficient.
2. I use the product ids as keys in the array. So in the foreach loop above, I do
Then I can access the object directly, just calling "$market->products[$id]" (or via a getProduct() function for those who fancy that). However, now I won't have a nice array of 0, 1 ,2 ,3 ,4 etc. Instead the keys will be something like 5, 8, 12, 40 56 etc. Are there any drawbacks of this? (Memory, or access speed or something...)
So, is this the way to go, or is there another "best practice" solution that I haven't thought of?
(Don't worry about programming misstakes since it's the concept I'm after.)
However, there is a scenario that probably is quite common, but I don't know the best way to solve: Finding a specific element in an array. (Before OOP I didn't need to worry since I could find it with SQL in the database.)
Let's say I have a supermarket with different products. I would create a Supermarket object, and add the products from the database as an array in the supermarket:
Code:
class Supermarket {
function Supermarket($ownerId) {
$results = 'SELECT * FROM products WHERE ownerId = ' . $ownerId;
foreach($results AS $resultItem) {
$this->products[] = new Product($resultItem);
}
}
}
Now I have an array of products that can be looped through with foreach($market->products AS $product). That's all good.
But how do I access a specific product? Let's say I want to change the price for the product with ID = 40. How do I access this product (to call its "setPrice()" function)?
I can see two solutions:
1. I implement a "getProduct($id)" function in the supermarket that loops through all products and returns the one with the correct $id. Seems inefficient.
2. I use the product ids as keys in the array. So in the foreach loop above, I do
Code:
$this->products[$resultItem['id']] = new Product($resultItem);
Then I can access the object directly, just calling "$market->products[$id]" (or via a getProduct() function for those who fancy that). However, now I won't have a nice array of 0, 1 ,2 ,3 ,4 etc. Instead the keys will be something like 5, 8, 12, 40 56 etc. Are there any drawbacks of this? (Memory, or access speed or something...)
So, is this the way to go, or is there another "best practice" solution that I haven't thought of?
(Don't worry about programming misstakes since it's the concept I'm after.)