1, Simple object creation
Copy Code code as follows:
The scope of the class final: will prevent subclasses from overriding this field
The scope of the method is abstract: Declared in a parent class, implemented in a subclass
Define class:
Class employee{
Defining fields
Private $name;
protected $title;
Public $wage;
Constant
Const PI = 3.1415926;
Static member Variable
private static $visitors = 0;
Defining constructors
function __construct () {
Using static member variables
Self:: $visitors + +;
echo "Constructor";
}
Defining destructors
function __destruct () {
echo "Destruct";
}
Declaring methods
Public Function Clockin () {
Working with Fields
echo "member $this->name";
}
When you access an attribute that an object does not own (such as a private field), the __get method or __set method is automatically invoked if the object user __get and __set the method
function __set ($property, $value) {
$this-> $property = $value;
}
function __get ($property) {
return $this-> $property;
}
}
Inheritance manager for a class inherits employee
Class Manager extends Employee {
function __construct () {
Call the constructor or method of the parent class
Parent::__construct ();
Parent::clockin ();
echo "Manager constructor";
}
}
Creating objects
$employee = new Employee ();
$employee->wage = 10000;
Using constants
echo Employee::P i;
Call method
$employee->clockin ();
$manager = new Employee ();
Instanceof Determines whether an object is an instance of a class, a subclass of a class, or implements a specific interface
if ($manager instanceof Employee) echo "Yes";
2, Advanced OO Features
(1) Object cloning
Copy Code code 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 was 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 was cloned.
Namea
Titlea
Nameb
Titlea
*/
(2) interface
Copy Code code 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 Code code as follows:
Abstract class, is a class that cannot be instantiated, and can only be a base class that inherits from 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 that will be adopted by tightly related objects, you can use abstract classes. If you want to create a feature that is used by unrelated objects, use an interface.
If you must inherit behavior from multiple sources, use an interface. PHP can inherit multiple interfaces, but cannot extend multiple abstract classes.
If you know that all classes share a common behavior implementation, use an abstract class and implement that behavior in it. Behavior cannot be implemented in an interface.