Here I have my first two classes that I have written in PHP and I'd like to know how I should improve my code.
Any comments will be helpful, thanks!
//Emil
Sweden
Code 1, User class.
The background thought was a simple way to work with logged in users (and different admin levels).
Code: Select all
<?php
/**********************************************************************
* - Made in the Eclipse IDE: http://www.eclipse.org
* - Tested on PHP 5.2
*
* Usage of the class User
*
* Attention!: user_id is a string and admin_level is an integer.
*
* -> On creation of the object:
* You can specify User ID and Admin Level when creating the object.
* Ex: $user_holder = new User("user_id", admin_level);
*
* -> SetUserID("user_id"):
* This will set/change the User ID.
*
* -> SetAdminLevel(admin_level):
* This will set/change the Admin Level.
*
* -> GetUserID():
* This will return a string with the User ID.
*
* -> GetAdminLevel():
* This will return an integer with the Admin Level.
*
**********************************************************************/
class User
{
private $user_id;
private $admin_level;
public function __construct($uid, $alvl = 0)
{
$this->SetUserID($uid);
$this->SetAdminLevel($alvl);
}
public function SetUserID($uid)
{
if ((string)$uid != "")
$this->user_id = (string)$uid;
}
public function SetAdminLevel($alvl)
{
if ((int)$alvl >= 0)
$this->admin_level = (int)$alvl;
}
public function GetUserID()
{
return $this->user_id;
}
public function GetAdminLevel()
{
return $this->admin_level;
}
}
?>Code 2, a simple shoppingcart class:
Code: Select all
<?php
/**********************************************************************
* - Made in the Eclipse IDE: http://www.eclipse.org
* - Tested on PHP 5.2
*
* Usage of the class ShoppingCart
*
* Attention!: product_id is a string and value is an integer.
*
* -> AddItem("product_id"):
* This will add an item to the cart.
* If the item already exists, the quantity of that item will increased by 1.
*
* -> DeleteItem("product_id"):
* This will remove an item from the cart.
* Remove as in it will be gone, if you want to change the quantity see 'SetQuantity'.
*
* -> GetItems():
* This will return an array with all the items in the cart.
*
* -> SetQuantity("product_id", value):
* This will change the quantity of the item to value.
* If value is 0 the item will be automatically removed.
*
* -> GetQuantity("product_id"):
* This will return the quantity of the item.
*
*
* Design of the cart array:
* _______________________
* | Product ID | Quantity |
* | (string) | (int) |
* |------------|----------|
* | exampleid1 | 5 |
* |- - - - - - | - - - - -|
* | exampleid2 | 3 |
* |- - - - - - | - - - - -|
* | exampleid5 | 13 |
* -----------------------
**********************************************************************/
class ShoppingCart
{
private $cart = array();
public function AddItem($product_id)
{
if (array_key_exists($product_id, $this->cart))
$this->cart[$product_id] += 1;
else
$this->cart[$product_id] = 1;
}
public function DeleteItem($product_id)
{
if (array_key_exists($product_id, $this->cart))
unset($this->cart[$product_id]);
}
public function GetItems()
{
return array_keys($this->cart);
}
public function SetQuantity($product_id, $value)
{
if (array_key_exists($product_id, $this->cart))
{
if ((int)$value > 0)
$this->cart[$product_id] = (int)$value;
else
unset($this->cart[$product_id]);
}
}
public function GetQuantity($product_id)
{
if (array_key_exists($product_id, $this->cart))
return $this->cart[$product_id];
}
}
?>