Forum Moderators: coopster

Message Too Old, No Replies

a basic PHP 5 OOP structure question from a newbie

         

formasfunction

9:16 pm on Dec 31, 2007 (gmt 0)

10+ Year Member



This is a bit of a loose question but I'm just getting into OOP on PHP 5 and I'm trying to wrap my head around a couple conceptual parts of it. I'm about halfway through two books on the matter and I understand the basics of creating classes, methods, polymorphism, etc.

Here's what I'd like to know, with a pseudo code example. Say a user is logged in with an account and they have a list of grocery items. I'm hitting the DB and feeding each line of the returned info into an object like so:


class GroceryItem {
protected $_name;
protected $_id;
protected $_price;

public function __construct($attributes){
$this->$_name = $attributes['name'];
$this->$_id = $attributes['id'];
$this->$_price = $attributes['price'];
}

public function getName(){
return $this->_name;
}

public function getId(){
return $this->_id;
}

public function getPrice(){
return $this->_price;
}

public function printOut(){
return "name=". $this->getName()
." price=$". $this->getPrice()
."<a href='/process.php?removeItem=". $this->getId() ."'>remove</a><br />";
}
}

//Table "item" has columns id,name,price,user_id
$q = "SELECT * FROM item WHERE user_id = 1";
$result = $database->query($q);
while($array = @mysql_fetch_assoc($result)){
$item = new GroceryItem($array);
echo $item->printOut();
}

/* Sample output:
name=Pickles price=$5 remove
name=Eggs price=$2 remove
name=Chips price=$3 remove
*/

So, essentially what you'd have is a list of all the items in their cart with a link to remove them if they wanted. The link goes to process.php which would see the GET var, grab the item id and use that to remove the item.

My question is, how exactly should this removal work? Right now what I'm doing is I have a static function in the GroceryItem class that I'm passing the id to like so:


//in GroceryItem class
static function remove($id){
global $database;
$q = "DELETE FROM item WHERE id =".$id;
return mysql_query($q, $database->connection);
}

//in process.php
if(isset($_GET['removeItem']){
$itemId = $_GET['removeItem'];
GroceryItem::remove($itemId);
}

But should I somehow be passing the grocery item objects from one page to the next and using a nonstatic method like so?


//in GroceryItem class
public function remove(){
global $database;
$q = "DELETE FROM item WHERE id =".$this->getId();
return mysql_query($q, $database->connection);
}

I'm a bit confused about how objects should be passed around, if at all. It seems like if I was passing the objects as a whole, I'd be able to more easily manipulate their contents with methods like setName() or setPrice() without hitting the DB all the time. Also, just for the sake of argument, lets say I want to keep track of the number of items the user has as a separate field in the DB. When I remove an item this needs to decrease the number in the DB by one. How would this look? Like this?


//in GroceryItem class
static function decreaseTotal(){
global $database;
$q = "UPDATE user SET total_items = total_items -1 WHERE id = 1";
return mysql_query($q, $database->connection);
}
static function remove($id){
global $database;
$q = "DELETE FROM item WHERE id =".$id;
if(mysql_query($q, $database->connection)){
return self::decreaseTotal();
} else {
return false;
}
}

OR would you instead do it in process.php like this:

//in process.php
if(isset($_GET['removeItem']){
$itemId = $_GET['removeItem'];
GroceryItem::remove($itemId)
GroceryItem::decreaseTotal();
}

I'm definitely a newbie so I appreciate any help you're able to give me.

coopster

11:46 pm on Jan 11, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I'm a bit confused about how objects should be passed around, if at all. It seems like if I was passing the objects as a whole, I'd be able to more easily manipulate their contents with methods like setName() or setPrice() without hitting the DB all the time.

I don't pass the objects around, I leave it on the server stored in a session, whether that session be a filesystem based session or in the database, I store it on the server and use the PHP garbage collection routine to handle how I (sometimes analyze and process and eventually ...) purge abandoned data. "Hitting" the filesystem or the database versus bandwidth consumption and security issues. Passing all that data is just more information that the end user has to wait for every round trip to the server. Also, if you have a lot of information, you may be pushing byte limitations in regards to GET/POST data as well as COOKIEs. Use your server, that's the reason it is called a server ;)

Also, just for the sake of argument, lets say I want to keep track of the number of items the user has as a separate field in the DB. When I remove an item this needs to decrease the number in the DB by one. How would this look? Like this?

Here, I would not even considering storing that number -- a synchronization mess if you ask me. If your cart class is setup correctly you can store the number of items in a variable or better yet, count [php.net] the item objects in your cart.

formasfunction

6:49 am on Jan 23, 2008 (gmt 0)

10+ Year Member



I appreciate the advice. They sync problems you addressed at the end hadn't occurred to me and I've changed my scripts appropriately.

phnord

5:30 am on Jan 25, 2008 (gmt 0)

10+ Year Member



This is a good read that introduces objects in PHP5 : [ibm.com...]

... and then more advanced stuff like abstract classes/functions : [ibm.com...]