Re: classes?
| From: | Matthew Brealey | Date: | Thu, 16 Nov 2000 16:28:28 +0000 |
| Subject: | Re: classes? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-25718@lists.php.net to get a copy of this message | ||
> On Thu, 16 Nov 2000, Kurth Bemis wrote:
>
> > ok all i'm just starting to get involved with classes...and have a slight
> > problem......i don't understand how to access functions..i have been
> > reading the manual at php.net but that confuses even more....take a look at
> > some code i wrote...
> >
> > print "<a href=\"$PHP_SELF?product=$product&quanity=1\">$title -
> > $price</a>";
> >
> > thats to add a item to my cart......
> >
> > the class function looks like this...
> >
> > function add_item($table, $session, $product, $quantity) {
> > $qty = $this->check_item($table, $session, $product);
> > if($qty == 0) {
> > $query = "INSERT INTO $table (session, product, quantity)
> > VALUES ";
> > $query .= "('$session', '$product',
> > '$quantity') ";
> > mysql_query($query);
> > } else {
> > $quantity += $qty;
> > $query = "UPDATE $table SET quantity='$quantity' WHERE
> > session='$session' AND ";
> > $query .= "product='$product' ";
> > mysql_query($query);
> > }
> > }> >
> > my question is how to i pass that vars that i need to the class? am i
> > making sense here?
> >
> > ~kurth
> > Kurth Bemis - Network/Systems Administrator,
> USAExpress.net/Ozone Computer
> >
> > There is no sin except stupidity. -- Oscar Wilde
> >
> > kurth@usaexpress.net | http://www.usaexpress.net/kurth
> > PGP key available - http://www.usaexpress.net/kurth/pgp
> >
> > Fight Weak Encryption! Donate your wasted CPU cycles to
> Distributed.net
> > (http://www.distributed.net)
> >
>
> First of all, don't use $PHP_SELF when dealing with things as vital as a
> shopping cart.
No, that's fine. E.g.,
Baked beans <a href>Buy</a>
Cabbage <a href>Buy</a>
There is no problem here - obviously GET is unsuitable for passwords,
logging in, credit card numbers, etc., but for this it's fine, because
there's really no problem with a user 'faking' a purchase - all it does
is mean they've got more in their basket, which they could do by
clicking anyway.
But I would do it like this:
<a href=\"$PHP_SELF?action=add&product=$product&quantity=1\">$title -
$price</a>
require_once "cart.php"; // If you're using PHP3, use require instead
switch ($action) {
case add:
$cart=new shoppingcart($session);
$cart->add($product,$quantity)
break;
default: // No action specified - display default page
// Default page goes here
}
and then
class sqlobject {
var password=''; // e.g., hello :-)
var username=''; // e.g., marcus
var hostname=''; // e.g., crap.com
function sqlobject($database) { // constructor
if (!$database) die("No database was specified: terminating");
mysql_connect($this->hostname,$this->username,$this->password);
mysql_select_db($database);
}
function query($query) {
mysql_query($query) or die(mysql_error());
}
/* for all the other functions; e.g., mysql_fetch_array you should have separate functions */
}
class shoppingcart {
function shoppingcart($session) { // Put all stuff you want to run on instantiation in here
if (!$session) die("No session was specified");
$this->session=$session;
$this->sql=new sqlobject("your_database_name"); // Change this
} function check_item($product) {
if (!$product) die("No product was specified: terminating");
/* Your code goes here */
}
function add($product,$quantity) {
if (!$product) die("No product was specified: terminating");
if (!isset($quantity)) die("No quantity was specified: terminating");
$qty = $this->check_item($product);
if($qty == 0) {
$query = "INSERT INTO $table (session, product, quantity)
VALUES ";
$query .= "('$this->session', '$product',
'$quantity') ";
$this->sql->query($query); } else {
$quantity += $qty;
$query = "UPDATE $table SET quantity='$quantity' WHERE
session='$this->session' AND ";
$query .= "product='$product' ";
$this->sql->query($query);
}
}
}
Some comments: you shouldn't expose your code to the details of your
database implementation: hence the sql object - that way, when you move
to another dtabase you can easily change.
It also reduces errors by removing common errors - invalid password,
forgetting to select a database etc.
You should validate every piece of input - at the top of the function,
check that all the parameters passed are ok - assume the worst.
You are passing things to functions that shouldn't really be passed -
the session id should be a property of the whole object, and hence by
putting it in the constructor, you don't need to pass it to every
function.
I'm not sure why your passing $table to the function - because I'm not
sure what $table is. It might be that the fact that $table changes
indicates questionable database design (do you really want so many
separate tables - won't this make it hard to query your data?); if you
really do want separate tables, then $table should be passed to the
constructor:
function shoppingcart($session,$table) { // Put all stuff you want to run on instantiation in here
if (!$session) die("No session was specified");
if (!$table) die("No table was specified");
$this->session=$session;
$this->sql=new sqlobject("your_database_name"); // Change this
$this->table=$table;
}
and then:
$query = "INSERT INTO $this->table (session, product, quantity)
VALUES ";
$query .= "('$session', '$product',
'$quantity') ";
$this->sql->query($query);
-- Random fortune
Is a tattoo real, like a curb or a battleship? Or are we suffering in Safeway?