1. Simple Object Creation
Copy codeThe Code is as follows:
// Class scope final: this field will be blocked from being overwritten by subclass
// The scope of the method abstract: declared in the parent class, implemented in the subclass
// Definition class:
Class Employee {
// Define Fields
Private $ name;
Protected $ title;
Public $ wage;
// Constant
Const PI = 3.1415926;
// Static member variable
Private static $ visitors = 0;
// Define the constructor
Function _ construct (){
// Use static member variables
Self: $ visitors ++;
Echo "constructor ";
}
// Define the destructor
Function _ destruct (){
Echo "destruct ";
}
// Declaration method
Public function clockIn (){
// Use fields
Echo "Member $ this-> name ";
}
// When accessing a property that an object does not possess (such as a private field), if the object user _ get and _ set methods, the _ get method or _ set method is automatically called.
Function _ set ($ property, $ value ){
$ This-> $ property = $ value;
}
Function _ get ($ property ){
Return $ this-> $ property;
}
}
// Class inheritance Manager inherits Employee
Class Manager extends Employee {
Function _ construct (){
// Call the constructor or method of the parent class
Parent: :__ construct ();
Parent: clockIn ();
Echo "Manager constructor ";
}
}
// Create an object
$ Employee = new Employee ();
$ Employee-> wage = 10000;
// Use Constants
Echo Employee: PI;
// Call Method
$ Employee-> clockIn ();
$ Manager = new Employee ();
// Instanceof determines whether an object is an instance of a class or a subclass of a class, or whether a specific interface is implemented
If ($ manager instanceof Employee) echo "Yes ";
2. advanced OO features
(1) object cloning
Copy codeThe Code is as follows:
// Object cloning
Class ClassA {
Private $ name;
Private $ title;
Public function setName ($ name ){
$ This-> name = $ name;
}
Function getName (){
Return $ this-> name;
}
Public function setTitle ($ title ){
$ This-> title = $ title;
}
Public function getTitle (){
Return $ this-> title;
}
Function _ clone (){
Echo "I have been cloned", "<br> ";
}
}
$ ClassA = new ClassA ();
$ ClassA-> setName ("NameA ");
$ ClassA-> setTitle ("TitleA ");
$ ClassB = clone $ classA;
$ ClassB-> setName ("NameB ");
Echo $ classA-> getName (), "<br>", $ classA-> getTitle (), "<br> ";
Echo $ classB-> getName (), "<br>", $ classB-> getTitle (), "<br> ";
/* Output
I have been cloned.
NameA
TitleA
NameB
TitleA
*/
(2) Interface
Copy codeThe Code is as follows:
// Interface
Interface IPillage {
Function method ();
}
Class ClassC extends ClassA implements IPillage {
Function method (){
Echo "inteface method ";
}
}
$ ClassC = new ClassC ();
$ ClassC-> method ();
// Inteface method
(3) abstract class
Copy codeThe Code is as follows:
// Abstract class, which cannot be instantiated and can only be used as the base class inherited by other classes
Abstract class BaseClass {
Protected $ name;
Abstract function method ();
}
Class ChileClass extends BaseClass {
Function method (){
Echo "method ";
}
}
$ Child = new ChileClass ();
$ Child-> method ();
// Output method
Note:
If you want to create a model, this model will be used by closely related objects, you can use abstract classes. If you want to create functions used by irrelevant objects, use interfaces.
If the behavior must be inherited from multiple sources, the interface is used. Php can inherit multiple interfaces, but cannot extend multiple abstract classes.
If you know that all classes share a public behavior implementation, use an abstract class and implement this behavior in it. Actions cannot be implemented in interfaces.