Submit Your Site For Free!

Email Address:
* URL:
*
*Indicates Mandatory Field

Terms & Conditions

Dev Newz
FlashNewz
DevWebPro










An Introduction To Classes In PHP

By Dragos Mincinoiu
Expert Author
Article Date: 2002-04-25

In this article, I will present a method for creating a very simple shopping cart in PHP. I assume you have a basic understanding of PHP or knowledge of an object- oriented language like C++ or Java. I will first introduce object orientation in PHP, and I guess many of you feel comfortable with this topic, so you may skip on to the section that deals with object oriented code.

Object-oriented PHP
If you have used an object oriented language before, you know that the subject is complex. I will present object-oriented PHP in a very simplistic manner but enough that you can learn to use objects in your scripts in a matter of minutes. Like any object-oriented language, PHP involves using classes to define objects and then using object instances to perform a task. The main purpose of a class is to provide instructions that define the functionality of an object. The class "Fruit" will provide information on a fruit such as shape and color. Let's look at the definition of a class called Fruit.

<?

class Fruit {

// these are the instance variables
var $shape;
var $color;

// this is the constructor
function Fruit($shape=NULL, $color=NULL) {
$this->color = $color;
}

// the remaining functions are member functions
// to set or retrieve the state of the object
//ACCESSOR MTHODS
function getColor() { return $this->color; }
function getShape() { return $this->shape; }

//MUTATOR METHODS
function setColor($color) { $this->color = $color; }
function setShape($shape) { $this->shape = $shape; }

//A PRINT UTILITY
function printVar () {
echo ". variables are: ($this->shape) and ($this ->color)";
}
}

?>

The class Fruit has two instance variables ($shape and $color), a constructor (function Fruit()), accessor and mutator functions. The instance variables describe the properties of a Fruit object. The constructor is used to create new Fruit objects. Constructors of a class always have the same name as the class. Here in the Fruit class, the constructor can take two optional parameters to create a new Fruit. The parameters are optional because they are each given a default value (null in our case) in the definition of the constructor. The member functions either get or set the values of the instance variables. Here is an example of how to create and use Fruit objects:

<?

include ("fruit.php");

$dragosFruit = new Fruit("yellow", "blue");
$getaFruit = new Fruit(null, "green");

echo "geta's color is: " . $getaFruit->getColor() . "";
echo $dragosFruit->getColor();
echo "" . Fruit::count();

$myFruit = new Fruit("cube", "blue");
$myFruit->printVar();

echo "You have: " . Fruit::count() . " fruits!!!!";

?>

If we wanted to extend the class fruit by including more specific information about the fruit, we could extend the class.

<?

class Apple extends Fruit {

// new instance variable
var $fresh;

function Apple($shape=null, $color=null, $fesh=true) {
$this->shape = $shape;
$this->color = $color;
$this->fresh = $fresh;
}

// new member functions
function getFresh() { return $this->fresh; }
function setfresh($fresh) { $this->fresh = $fresh; }
}

?>

Our Cart
Since you now understand the basics of object-oriented PHP, lets look at designing a shopping cart. Do you really understand what a shopping cart is? Below are the steps for purchasing online:

• Browse or search through a product catalog
• Put the products you want to buy in your cart
• Review the products in your cart and made any necessary changes
• entered your personal information and purchase the items Just like the cart you push in a grocery store, an online shopping cart simply stores products you would like to purchase. In your shopping cart you should have stored at least an item id (unique) and the quantity of the product. The basic operations performed on a shopping cart are adding and removing products. Another important function of the cart will be to display the content of the cart. We are going to use our recently acquired knowledge of classes to implement our shopping cart as a class.

class Cart {

// holds products with quantities
var $Products;

// add $qty product(s) to the cart
function add($id, $qty) {
if($qty > 0) $this->Products[$id] += $qty;
}

// remove $qty product(s) from the cart
function remove($id, $qty) {
if($qty > 0)

//lets first check if we have sufficient in stock
//than remove products
if($this->Products[$id] >= $qty) $this->Products[$id] -= $qty;
}
}

Our class contains the variable - $Products. This variable is an associative array where the key is the product id and the value is the quantity. The two member functions allow us to add/remove 'x' number of products to/from the cart.

Above we have created a shopping cart for an online store. This is only a simplistic view of the cart that will be presented in our future articles, where we will actually start with a catalog than expand our implementation of our shopping cart.

About the Author:
Dragos Mincinoiu is a staff writer for iEntry.




Newsletter Archive | Article Archive | Submit Article | Advertising Information | About Us | Contact

Dev Newz is an iEntry, Inc.® publication - 1998-2008 All Rights Reserved Privacy Policy and Legal