|
What is PHP class?
class
A class is a collection of variables and functions working with these variables. A class is defined using the following syntax:
<?php
class Cart {
var $items ; // Items in our shopping cart
// Add $num articles of $artnr to the cart
function add_item ( $artnr , $num ) {
$this -> items [ $artnr ] += $num ;
}
// Take $num articles of $artnr out of the cart
function remove_item ( $artnr , $num ) {
if ( $this -> items [ $artnr ] > $num ) {
$this -> items [ $artnr ] -= $num ;
return true ;
} elseif ( $this -> items [ $artnr ] == $num ) {
unset( $this -> items [ $artnr ]);
return true ;
} else {
return false ;
}
}
}
?>
This defines a class named Cart that consists of an associative array of articles in the cart and two functions to add and remove items from this cart.
|