In PHP object-oriented language, class definition and Object Instantiation operations, as well as special usage of constructor and destructor, php object-oriented language
Class Definition and Object Instantiation
<? Php
// The members in the class belong to the object
Class Hero {
Public $ name; // member variable
// Member attributes (in strong language)
Protected $ blood;
Private $ attack;
// Member Method
Function skill (){
Echo "$ this-> name <br> control ";
}
}
$ S = new Hero (); // create a Hero
$ S-> name = "Riven"; // assign a value to the hero
Echo "$ s-> name <br>"; // output the hero name
$ S-> skill ();
Constructor and destructor
// Function: Initialize some Members in the class (custom initial values of parameters)
Class Ren {
Public $ name;
Public $ age; // type used in strongly typed languages
// Old version definition usage
/* Public function Ren (){
Echo "constructor ";
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;
?>