Welcome to WebProNews Breaking eBusiness and Search News
Advertise | Newsletter | Sitemap | News Feeds News Feed 
 WebProNews Search Part of the iEntry network iEntry inc. 

An Introduction To Classes In PHP

Dragos Mincinoiu
Expert Author
Published: 2002-04-25

WebProNews RSS Feed


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.

Receive Our Daily Email of Breaking eBusiness News


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

WebProNews RSS Feed

More Articles

Contact WebProNews
Advertisement





TOP NEWS

Targeted Information for Business
WebProNews is part of the iEntry network

Internet Business: Marketing: Small Business:
WebProNews MarketingNewz SmallBusinessNewz
WebProWorld AdvertisingDay PromoteNews
EcommNewz SalesNewz EntrepreneurNewz

Software: Search Engines: Web Design:
WebMasterFree Jayde B2B DesignNewz
NetworkingFiles SearchZA FlashNewz
SecurityConfig SearchNewz WebSiteNotes

Developer: IT Management: Security:
DevWebPro ITManagement SecurityProNews
DevNewz SysAdminNews SecurityConfig
TheDevWeb NetworkingFiles NetworkNewz

The iEntry Network consists of over 100 web publications reaching millions of Internet Professionals. Contact us to advertise.
eBUSINESS RESOURCES






 Advertise | Contact Us | Corporate | Newsletter | Sitemap | Submit an Article | News Feeds
 WebProNews is an iEntry, Inc. ® publication - $line) { echo $line ; } ?> All Rights Reserved
About WebProNews
WebProNews is the number one source for eBusiness News. Over 5 million eBusiness professionals read WebProNews and other iEntry business and tech publications.

WebProNews provides real-time coverage of internet business.

Free Email Newsletters:
WebProNews SearchNewz
WebProWorld DevWebPro
Marketing SecurityNews
Plus over 100 other newsletters!

Send me relevant info on products and services.


WebProWorld
Ten most recent posts.

NetworkingFiles
Featured Software

WebProNews in the News
View all recent mentions of WebProNews from around the world!

Recent Articles On ...
Google eBusiness
Yahoo Ask Jeeves
MSN Blogs
Search Engines Blogging
Affiliate Programs Marketing
eCommerce Advertising
eBay Sun Microsystems
AOL Adsense
Microsoft Adwords
Oracle IBM
Amazon Apple
SEM Mac
SEO iPod
Adsense XBox
PR Adobe



iEntry.com WebProWorld RSS Feed WebProWorld Contact WebProNews Print Version Email a friend Bookmark us