First, the object-oriented three kinds of characteristics, respectively, are encapsulation, inheritance and polymorphism.
1, Encapsulation: Encapsulation is the core of object-oriented thinking, the object's properties and behavior to encapsulate, do not need to let the outside world know the specific implementation details, this is the encapsulation of thought.
2, Inheritance: Inheritance is mainly to describe the relationship between classes and classes, through inheritance, you can not need to rewrite the original class, the function of the original class to expand.
3, polymorphism: refers to the same operation acting on different objects, will produce different execution results.
Second, class and object
In short, a class is abstract, whereas an object is specific; A class is a universal attribute of a species, whereas an object is a subdivision, specific to an entity object.
In object-oriented, the core is the object, in order to create an object, first define a class.
1. Definition of class
Class Name {
member properties;
Member methods;
}
2, the creation of objects, also known as instantiation.
$ Object name = new class name ([parameter 1][parameter 2][...]);
Iii. Encapsulation of Classes
The encapsulation of a class is defined as a class. Privatization of properties in a class, which is decorated with the private keyword, can only be accessed in the class in which it resides. PHP provides two ways to allow outside access.
1. Access private properties through the GetXXX () and setxxx () methods
GetXXX gets the property value, Setxxx sets the property value.
Class person{
Private $name;
Public Function GetName ()
{
return $this->name;
}
Public Function SetName ($value)
{
$this->name = $value;
}
2. Access private properties through the __get () and _ _set () methods
These two methods are automatically called by PHP, and the function is the same as above.
3. Public, protected, and private modifiers
Public: Common modifier, accessible to all members.
Protected: Protects the member modifier, which cannot be accessed by the decorated members of the class, but can be accessed and read and written by the quilt class.
Private modifier, which is visible to members within the same class, unrestricted access, but does not allow external code access.
4, this
It represents the object itself, the current object.
$this-property name;
Iv. construction methods and methods of destruction
Each class has a constructor that automatically invokes the class as it is instantiated and initializes the members of the class.
It corresponds to a destructor, which is called automatically before the object is destroyed, and is used to complete the cleanup.
1. Construction method
Modifier function __construct (argument list) {
Initialization
}
2, the Destruction method
Functions __destruct () {
Cleanup operations
}
V. Class constants and Static members
1. Class constants
Const PI = 3.1415926;
Using the CONST keyword to declare a constant, the constant name does not require a $ symbol and is initialized at the same time as the declaration. Constants can be shared by all objects of the class.
2. Static members
The data is shared by all objects of the class, not any objects, only classes.
Access modifier static variable name;
Access modifier static method name () {}
Vi. Succession
1, the concept of inheritance
In the program, inheritance describes the relationship between things, through inheritance can make a relationship between the various things system. Cats and dogs, for example, are animals.
Class subclass name extends parent class name {
Class Body
}
2. Overriding the parent method
In an inheritance relationship, subclasses automatically inherit the methods defined in the parent class, but sometimes the subclasses need to make some modifications to the inherited methods, that is, the methods of the parent class are overridden.
3. Final keyword
When a class in PHP is decorated with the final keyword, the class cannot be inherited, that is, the subclass cannot be derived.
When a class method is modified by the final keyword, the subclass of the class cannot override the method.
VII. abstract classes and interfaces
1. Abstract class
When defining a class, some of the methods required in it are temporarily not fully defined, but instead are implemented with classes inherited by them, and abstract classes are used at this time.
Abstract class class Name {
Class Member
}
Abstract methods only method names have no method body.
Abstract function method name ();
2. Interface
If all the methods in a class are abstract, you can define the class in another way, that is, the interface. When defining an interface, you need to interface the keyword. Defining an interface is similar to defining a standard class, but all of the defined methods are empty.
Interface class Name {
Public function A ();
Public function B ();
}
Viii. polymorphic
When designing a method, it is usually hoped that the method has some universality. For example, to implement an animal called method, because each animal's call is different, so you can receive different animal type parameters in the method, when the incoming contradiction is issued by the cat Barking, the dog is barking.
PHP Object-oriented summary