The definition of the class and the instantiation of the object
The members of the
<?php
//class are
class hero{
public $name that belong to the object;//member Variable
// Member properties (in strongly typed languages)
protected $blood;
private $attack;
//member methods
function skill () {
echo "$this->name<br>control";
}
}
$s = New Hero ();//Create a hero
$s->name = "Riven"; //Assign Heroes
Echo "$s->name<br>";//output Hero
$s->skill ( );
constructors, destructors
//role: Initialize some members of a class (parameter custom initial value)
Class ren{
public $name;
public $age;//Use type
in strongly typed languages
Old version definition Usage
/* Public Function Ren () {
echo "is constructing function";
Public Function Ren ($v) {
$this->age = $v; */
PHP new version definition usage
Public function __construct ($v) {
$this->age = $v;
}
Public Function __destruct () {
echo "Destroying files";
}
}
$s = new Ren (20);
Echo $s->age;
?>
About PHP Object-oriented-class definition and instantiation of objects and special usage of construction and destructor functions