Look at an example
The code is as follows |
Copy Code |
<?php Class Cart { var $items; //Shopping cart Items //Put $num $a Rtnr into the car function Add_item ($ARTNR, $num) { $this->items[$artnr] + = $num; } //Take $num $artnr out of the car function Remove_item ($ARTNR, $num) &nbs p; { if ($this->items[$artnr] > $num) { $this->items[$artnr]-= $num; return true; } else { return false; } } } |
The above type defines a class called Class, which is a class consisting of an array of shopping carts and two items that are used to add and remove merchandise functions from a shopping cart.
Declares the class we want to see how the example uses
Create an object,
The code is as follows |
Copy Code |
$obj 111cn_.net = new Cart; |
Instance
The code is as follows |
Copy Code |
<?php Class session { Public Function __construct () { Echo ' constructor execution '; } Public Function __destruct () { echo ' destructor execution '; } } $OBJ = new Session; Unset ($OBJ);//Delete object, __destruct () will be invoked Output Constructor execution /destructor Execution ?> |
$obj 111cn_.net->add_item (1,2);
This is the property and method of the calling class.
The code is as follows |
Copy Code |
<?php Class session { Public $age = 20; Public $sub = ' www.111cn.net '; Public Function __clone () { $this->sub=clone $this->sub; } } Class Session 2 { Public $value = 5; } $s = new session; $s->sub=new session 2; $s 2 = clone $s; $s->sub-> $value = 10; echo $s 2->sub-> $value; ?> |
Invoke PHP Class Property and method instance two
The code is as follows |
Copy Code |
<?php Class session { Public $age = 20; Public $sub = ' 111cn.net '; Public Function __clone () { $this->sub=clone $this->sub; } } Class Session 2 { Public $value = 5; } $s = new session; $s->sub=new session 2; $s 2 = clone $s; $s->sub-> $value = 10; echo $s 2->sub-> $value; ?> |
This site original tutorial reproduced annotated source Www.111cn.net