1. Defining classes
class ren{public$name// define member variable var$sex; function Say () // definition method {echo "This person is speaking";}}
2. Instantiation
$ren New Ren (); // instantiation of $ren // Calling member variables $ren->sex= "female"; $ren->say (); // Calling Methods
3.this references
class ren{ public $name public $sex public function Say () { echo ->name." This man is talking "; // this represents object not class }} $ren = Ren (); $ren ->name= "Zhang San" ; $ren ->say ();
4. Constructors
class ren{ public $name public $sex function __construct (," Span style= "color: #800080;" > $sex ) // constructor { $this ->name= $name ; $this ->sex = $sex ;}} $ren = new Ren ("John Doe", "male");
5. Destructors
function __destruct () // Destroy object cast memory, close connection, etc. Auto execute {echo ' The object is about to be destroyed. ";}
6. Encapsulation
classren{Private $name;//to private a member variablePrivate $sex;Private $age;//Encapsulation: Methods for accessing member variables Public functionSetage ($age) { if($age> &&$age<80) { $this->age =$age; } } //Encapsulation: Simple method assignment, automatic invocation Public function__set ($name,$value) { if($name= = "Age") { if($value>18 &&$value<80) { $this-$name=$value; } } Else { $this-$name=$value; } } //encapsulation: A simple way to get a value Public function__get ($name) { return $this-name; } }$ren=NewRen ("John Doe", "male")$ren->name = "What"; Echo $ren->sex;
7. Inheritance
class extends ren{// Parent class method overrides function Say () {parent:: Say (); // Echo "This is the method of the subclass";}}
8. Keywords
Final is used to modify the class, and if a class is defined as a final class, the class cannot be inherited.
Final class ren{}
Static statically.
classstudent{Static Public $class;//Defining static VariablesStatic functionSay ()//Defining static Methods{Echo"AAAA";}} Student::$class= "Hello";//calling static variablesEchoStudent::$classStudent::$class;//calling a static method
Const defines constants in a class
Const // Defining constants
Echo Studen::ss
PHP Language-Object oriented