Inheritance of Classes
A class can inherit the properties and methods of another class
the inheritance of classes in PHP is only single inheritance, there is no multiple inheritance, that is, a class can inherit only one class, and cannot inherit multiple classes
However, there are several ways to allow a class to inherit multiple, for example: class B Inherits Class a, class C inherits class b . Then you inherit class a and class b .
Inheritance needs to use a keyword
Extend
Grammar:
Class class name extends the inherited name {}
the essence of inheritance : inheritance is not to copy the parent class to the subclass, but through the inheritance chain form, first to subclass, if not found will go to the parent class to find!
overriding override
Meaning: When the name of the member of the subclass is the same as the member of the parent class, when the subclass is accessed, the subclass overrides the members of the parent class, that is, the form of the inheritance chain as mentioned earlier, find the son first, then find the father, who to use first.
An interview question:
$this always represent the object of your class?
No!
$this exactly which class the object is represented by, not by which class the $this code is written, but by the execution environment of the current method, and the execution context , which can be passed up or down!
access Control modifiers
There are three access control modifiers
public: public, within the current class, within the inheritance chain class and outside the class can be accessed!
protected: protected, inside the current class and within the inheritance chain class can be accessed, but outside the class cannot be accessed!
private: private, only within the current class can be accessed!
Generally within the project class inside the members will try to hide, that is, private, and then open a few public port method, let the user access
If the parent class has a private member, the subclass can inherit, but cannot directly access
final End Class
The value of this class is smart instantiation, not inherited, but can inherit others
syntax: add final to class
Final Method
After adding the final keyword to the front of a method name , it becomes the final method!
The final class means that the class cannot be inherited, and the final method means that the class cannot be overridden!
Therefore, the meaning of the final method is that when the class is inherited, the final method inside the parent class cannot be overridden!
Abstract
Abstract class
Meaning: can only be inherited by other classes, not instantiated
the role of abstract classes
1, can complete the ordinary class inheritance, for other classes to provide the common code!
2, for the provision of the subclass must appear in the method members, the structure of the subclass, in the real project, is often guaranteed to complete a series of functions similar to the structure of a variety of operating classes to maintain consistency! We require these functionally similar classes to inherit from the same abstract class!
3, subclasses must have an abstract class of properties and methods, or it will be an error
interface Interface
An interface is a common way of doing things to the outside of an object!
Object encapsulation: The object to the inside of the definition code as far as possible to hide, the user does not need to know how the function is implemented, the user only need to operate the interface method provided by this class is OK!
in the The definition of an interface in PHP is, in fact, a purely canonical or prescriptive way of defining a common method that subclasses of that interface must implement!
Only two types of members can appear in an interface:
Interface constants: In fact, is the ordinary class constant, but appears in the interface only!
Abstract Method: must be declared as public(unlike abstract classes, abstract classes can define members of all types)
abstract methods do not need to use the abstract keyword, because the interface interface is not really a class, the method is the default abstract method!
As long as it inherits its subclass, it needs to declare constants and create methods.
comparison of interfaces and abstract classes
1, from the logical structure point of view, the interface can be regarded as a "subset" of the abstract class, you can specify the internal structure of the subordinate class, but only interface constants and public abstract methods inside the interface!
2, PHP does not support multiple inheritance, but supports multiple implementations, that is, a class can implement multiple interfaces at the same time! This is also the most essential difference between an interface and an abstract class!
In real projects, use more interfaces with the interface!
overload Overloading Introduction
Refers to the handling of inaccessible members, called Overloads of Members!
That is , when assigning a value to a property that does not exist, PHP defaults to support the property being reloaded into the internal members of the current object, which is the "overload" of the Property!
in fact, PHP to access the inaccessible members, you can take the appropriate action to deal with!
depending on the member being processed, the overloads can also be divided into: Property Overloading and method overloading!
Property Overloading
The system has several magic methods that are about property overloading
There are four of the 4 cases that handle attribute operations, respectively :
when assigning a value to an inaccessible property: __set ()
when getting the value of an inaccessible property:__get ()
when deleting an inaccessible property:__unset ()
when determining whether an inaccessible property exists:__isset ()
__set ( class name, Value );
__get(class name)
__unset(class name)
__isset(class name)
Overloading of methods
__call ()
The Magic method is automatically executed when an inaccessible non-static method (object method) is invoked!
The default behavior of the system is error:
Two parameters required
String method names, strings, need to enclose quotation marks
array of parameters for the method accessed by array parameter arrays
__callstatic ()
Trigger Time: This method is automatically executed when you access a static method that is not accessible! Note: You need to add the static keyword to the front of the method ! Because, when the static method of access does not exist, it should also be accessed by the class to the Magic method, so you need to add the static keyword!
the required parameters are the same as __call.
Magic methods and related magic constants
Magic Method
__invoke ()
Trigger Time: the Magic method is automatically executed when the object is called as a function or method! Not too much at the moment!
__tostring ()
Trigger Time: This method is automatically triggered when an object is used as a string.
Magic Constants
__class__
Represents the current class name
__method__
Represents the current method name!
__NAMESPACE__: Represents the current namespace name!
Late static binding
summarize The function of the static keyword:
1, can be used when defining local variables, become static local variables, and the life cycle of variables related
2, when defining a static member in a class, the meaning of the representation is that the member belongs to all the members that are shared by the object and should be managed by the class
3, representing "Current Class"!
Object-Oriented 2