Access control
Public: can be accessed in classes, subclasses, and outside of the class.
Protected (Protected): Accessible only in the class itself and in subclasses.
Private: can only be accessed in the class in which they are declared, and private class members cannot be accessed directly by subclasses or object instances of this class .
Abstract classes and methods
In some scenarios where the concept of inheritance is applied, it is not appropriate to create an instance of a parent class. In this case, it would be more appropriate to define the parent class as an abstract base class rather than a standard base class (abstract classes cannot be instantiated).
An abstract class defines some of the methods in the inheriting class (subclass) of this base class. The subclass will then be responsible for defining the true implementation of these methods.
The difference between an abstract class and a normal class is that if you attempt to create an object from an abstract class, there will be a fatal error that the abstract class is used to extend, and then we can create an instance of these derived classes.
Definitions of abstract classes and methods:
//the definition of an abstract class starts with the abstract keywordAbstract classpeople{protected $name= ""; //abstract method begins with abstract, and there is no method body for the abstraction method, ending with a semicolon Abstract Public functionSetName ($name); }//Sub-classways to implement abstract classesclassTeacherextendspeople{ Public functionSetName ($name){ $this->name =$name; }}
Attention:
1. when an abstract method is implemented in a subclass, its visibility must be higher or equal to the visibility of the definition of the abstract method. if the abstract method is public, the implementation method in the subclass must also be public. If the abstract method is protected (protected), then the subclass will only be protected or public.
2. We cannot define abstract methods as private (private), because a private method cannot be inherited .
3. In all cases, the implementation version of the method must also have the same number of parameters as the abstract method (the function signature must be identical).
4. all the abstract methods in the base class must be implemented in the subclass, or a fatal error will occur.
5. Attributes have no abstraction that the attributes in an abstract class are defined as normal properties, and subclasses can override properties in an abstract class, where the visibility setting of the property is consistent with the abstract method (see 1th).
This chapter blog will not periodically update all the knowledge about the object-oriented PHP, please look forward to ...
PHP Object-Oriented programming