Classes

Classes

The object has its own properties and can do with it different things. Class is not an object, but its definition (eg. The class is a dog, but not the specific copy). The class definition contains variables (object properties) and function (what you can do with these objects). By using such a convention programming time can be easier to describe the interactions between objects. Here’s how to define a sample class and examples of actions on it.

Example
<?php

// Start the class definition Basket – defining basket shop
class Cart
{

 // Variable definition – an associative array containing the numbers of                                                                                            articles and
                                                                         the number of
var $ items;

 // This method adds a given number of items to cart

function add ($number, $amount)

{

$ this->items [ „$number”] + = $amount;

}

// This method removes the given number of                                                                                            articles in the cart

function delete ($number, $amount)
{

if ($this->items [ „$number”]> $amount)

$ this->items [ „$number”] – = $amount;

else

$this->items [ „$number”] =0;

}

// This method displays the contents of cart

function display ()
{

while (list ($k, $v) = each ($this->items))
if ($v> 0)
echo „Article No. k$ – $v pieces<br> „;

}

}

// Example of use

$cart = new Cart;
$cart->add (’20 ‚, 2);
$cart->add (’12 ‚, 4);
$cart->add (’20 ‚, 5);
$cart->delete (’12 ‚, 4);
$cart->show ();
?>

To get to a variable or function which is a component class, use the operator „->”. Inside the function belonging to a class the variable $ this means is this object. You have to use it trying to appeal to a variable or function belonging to the class.
In PHP inheritance it is very simplistic. Inheritance is used to create new classes based on the other, expanding their capabilities. For our class, you can add a new attribute – the name of the owner.

Example

<?php

class first_cart extends Cart {

var $name;

function set_name ($ new_name)
{

$this->name = $new_name;

}
}

$cart = new First_cart;
$cart->set_name (‚Tom’);
$cart->add(‚2’, 34);
echo ‚owner’: .$cart->name. ‚<br/>’;
$cart->show ();

?>

The constructor is a function with the same name as the class, which sets the initial values of the variables (called automatically when creating the object). The designer can provide some value when creating the object. Here’s another version of the cart equipped with a constructor that sets the given name of the owner or the „Anonymous”:

Example

<?php

Class Auto_cart extends First_cart
{

function Auto_cart ($name = ‚Anonymous’)
{

$this->set_name ($name);

}
}

$cart = new Auto_cart(‚Owner’);
$cart->add(‚4’, 30);
echo ‚owner’: .$cart->name. ‚<br/>’;
$cart->show();

?>

 

Cookies help us deliver our services. By using our services, you agree to our use of cookies. More Info | Close