$item = new Items;
$item -> setName("Book");
$book = $item->getName();
$item -> setName("Pencil");
$Pencil = $item->getName();
class Items
{
var $item;
function setName($newname)
{
$this->title=$newname;
}
function getName()
{
return $this->title;
}
}
Showing posts with label php classes. Show all posts
Showing posts with label php classes. Show all posts
Wednesday, October 10, 2007
Simple PHP Class Example Part 2
/*Create a new Object*/
$pricelist = new PriceList;
/*Set item name and price*/
$pricelist-> setItem("Carrot",10);
/*Get Item information*/
$itemname = $pricelist->getItemName();
$itemprice = $pricelist->getItemPrice();
$iteminfo = $pricelist->getItemInfo();
/*Display Item Information*/
echo "Name: $itemname \n";
echo "Price: $itemprice \n";
echo "Item Information: $iteminfo \n";
/*
OUTPUT
Name: Carrot
Price: 10
Item Information: Item=Carrot, Price=10
*/
/* This is the class object */
/* The Class name is "PriceList"*/
class PriceList
{
var $item; // -> this will hold the item name
var $price; // -> this will hold the item price
var $iteminfo; // -> this will hold the item info
/* This function will set the item name and price and store them in "inteminfo"*/
function setItem($newitem, $newprice)
{
$this->item=$newitem;
$this->price=$newprice;
$this->iteminfo = "Item=".$this->item.", Price=".$this->price;
}
/* This function will return the item info of the object */
function getItemInfo()
{
return $this->iteminfo;
}
/* This function will return the item name of the object */
function getItemName()
{
return $this->item;
}
/* This function will return the item price of the obj*/
function getItemPrice()
{
return $this->price;
}
}
$pricelist = new PriceList;
/*Set item name and price*/
$pricelist-> setItem("Carrot",10);
/*Get Item information*/
$itemname = $pricelist->getItemName();
$itemprice = $pricelist->getItemPrice();
$iteminfo = $pricelist->getItemInfo();
/*Display Item Information*/
echo "Name: $itemname \n";
echo "Price: $itemprice \n";
echo "Item Information: $iteminfo \n";
/*
OUTPUT
Name: Carrot
Price: 10
Item Information: Item=Carrot, Price=10
*/
/* This is the class object */
/* The Class name is "PriceList"*/
class PriceList
{
var $item; // -> this will hold the item name
var $price; // -> this will hold the item price
var $iteminfo; // -> this will hold the item info
/* This function will set the item name and price and store them in "inteminfo"*/
function setItem($newitem, $newprice)
{
$this->item=$newitem;
$this->price=$newprice;
$this->iteminfo = "Item=".$this->item.", Price=".$this->price;
}
/* This function will return the item info of the object */
function getItemInfo()
{
return $this->iteminfo;
}
/* This function will return the item name of the object */
function getItemName()
{
return $this->item;
}
/* This function will return the item price of the obj*/
function getItemPrice()
{
return $this->price;
}
}
Saturday, September 22, 2007
Simple PHP Class Example
/* Simple Class Structure */
class MathOperation
{
var $value1;
var $value2;
function Sum($value1, $value2)
{
$result = $value1 + $value2;
return $result;
}
function Diff($value1, $value2)
{
$result = $value1 - $value2;
return $result;
}
function Prod($value1, $value2)
{
$result = $value1 * $value2;
return $result;
}
function Quot($value1, $value2)
{
$result = $value1 / $value2;
return $result;
}
}
/*
STRUCTURE : [object variable name] = new [class name]
$compute : is the variable name of the new class object that we created
MathOperation : is the name of the class that we creayed
*/
$compute = new MathOperation;
/*
STRUCTURE : [object variable name] -> functionname(parameter1, parameter2)
$sum : is the variable which would hold the result of the class function that we called
Sum() : is the class function that we called
2 : would be assigned to $value1
9 : would be assigned to $value2
thus, "$value1 = paramater1, $value2 = parameter2" and so on
*/
$sum = $compute->Sum(2,9);
$diff = $compute->Diff(5,6);
$prod = $compute->Prod(3,2);
$quot = $compute->Quot(7,3);
/*This will display the result of the above operation*/
echo "Sum(2,9) = $sum, Diff(5,6) = $diff, Prod(3,2) = $prod, Quot(7,3) = $quot";
/*
OUTPUT :
Sum(2,9) = 11, Diff(5,6) = -1, Prod(3,2) = 6, Quot(7,3) = 2.33333333333
*/
class MathOperation
{
var $value1;
var $value2;
function Sum($value1, $value2)
{
$result = $value1 + $value2;
return $result;
}
function Diff($value1, $value2)
{
$result = $value1 - $value2;
return $result;
}
function Prod($value1, $value2)
{
$result = $value1 * $value2;
return $result;
}
function Quot($value1, $value2)
{
$result = $value1 / $value2;
return $result;
}
}
/*
STRUCTURE : [object variable name] = new [class name]
$compute : is the variable name of the new class object that we created
MathOperation : is the name of the class that we creayed
*/
$compute = new MathOperation;
/*
STRUCTURE : [object variable name] -> functionname(parameter1, parameter2)
$sum : is the variable which would hold the result of the class function that we called
Sum() : is the class function that we called
2 : would be assigned to $value1
9 : would be assigned to $value2
thus, "$value1 = paramater1, $value2 = parameter2" and so on
*/
$sum = $compute->Sum(2,9);
$diff = $compute->Diff(5,6);
$prod = $compute->Prod(3,2);
$quot = $compute->Quot(7,3);
/*This will display the result of the above operation*/
echo "Sum(2,9) = $sum, Diff(5,6) = $diff, Prod(3,2) = $prod, Quot(7,3) = $quot";
/*
OUTPUT :
Sum(2,9) = 11, Diff(5,6) = -1, Prod(3,2) = 6, Quot(7,3) = 2.33333333333
*/
Content category
php,
php class,
php classes,
php object,
php objects
Subscribe to:
Posts (Atom)