A class is a complex data type, and this type of data mainly has attributes, methods, and two kinds of things.
Properties are actually variables that can hold data, which can be integers, strings, arrays, and even classes.
Methods are actually functions that are used to accomplish certain functions.
The properties and methods that refer to a class use the-a symbol.
Here is an example applet:
<?php
Define class Cart
Class Cart {
var $items; Items in the shopping cart
Add $num $artnr items to your shopping cart
function Add_item ($ARTNR, $num) {
$this->items[$artnr] + = $num;
}
Remove $num $artnr items from your shopping 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;
}
}
}
Example inheritance definition class Named_cart
Class Named_cart extends Cart {
var $owner;
function Set_owner ($name) {
$this->owner = $name;
}
}
Using the code of the class
$ncart = new Named_cart; Create a new shopping cart with a name
$ncart->set_owner ("Kris"); Give the shopping cart a name
Print $ncart->owner; Output the name of the owner of the cart
$ncart->add_item ("10", 1); (features inherited from the shopping cart class)
?>
This arrow can also be a function in the calling class such as Class A {function B () {echo ' A ';}} $a =new A; $a->b (); Output: A
There is also an arrow like this, which defines an array for example $array 1=array (' A ' =>5, ' B ' =>6); while ($arrayitem =each ($array 1)) {extract ($arrayitem); Echo (' <br/> '. $key. ' = '. $value); } Output: A=5 b=6
What do the arrows "," and "=" in PHP code mean?