PHP encapsulation and inheritance instance
Encapsulation
Encapsulate member methods and member attributes in the class to hide the details of attributes and methods, and restrict the access permissions of class members through public, protected, and private. The data is protected internally, only authorized member methods can be used to encapsulate members as much as possible.
Public: methods or attributes can be accessed in any scope and are default. If an access modifier is not specified for an attribute or method, it will be public.
Protected: this class and subclass can be accessed, and external objects cannot be called.
Private: it can only be accessed in this class, and neither the subclass nor the external object can be called. Methods or attributes marked by private can be redefined in the inheritance class. each class can only see its own private method.
The three modifiers should be sorted in a descending order like this: public → protected → private in scope, because the class encapsulates some attributes and methods, this encapsulation determines the "visibility" of data ", in this way, the defined attributes and methods cannot be modified but can only be called outside the class. this is the benefit of encapsulation and improves security.
Sample code:
Class myClass {
Public $ public = "Public"; // public attribute
Protected $ protected = "Protected"; // protected attribute
Private $ private = "Private"; // private attribute
Function say_Hello () {// public attribute
// Add content by yourself as an example
}
$ Obj = new myClass ();
Echo $ obj-> public;
// Echo $ obj-> protected;
// Echo $ obj-> private;
Run the above example to get a "Public", but when you remove the comments of // echo $ obj-> private;, you will get the following error: Fatal error: cannot access protected property myClass: $ protected in E: apachehtdocsexamplefile. php on line 13. We can see that we cannot define the attributes of the category class at will. we don't know which members exist in this class in the "outside", because these members may be invisible to other classes. Of course, if we must access or modify the attribute defined as "private", we can also use the system methods provided by PHP: _ get () and _ set ().
Inheritance
A class can inherit and have the member attributes and methods of another existing class. the inherited class is called the parent class or the base class, and the inherited class is a subclass. Use the extends keyword to implement the inheritance relationship. In general, there must be a "root cause" for inheritance. you may imagine that you have given birth to a son or daughter in the future, they will get some "things (attributes and methods)" from you, so that your "descendant" will hold all the characteristics of You (root.
Generate "root" class (parent class or base class)
Syntax: class father {
}
Generate "descendant" (subclass)
Syntax: class son extends father {
}
Example code of PHP extends class inheritance:
Class father {
Protected $ name;
Function _ construct ($ name) {// Constructor
$ This-> name = $ name;
}
Function work (){
Echo "{$ this-> name} I'm at work;
}
Function _ destruct () {}// destructor
}
Class son extends father {// inherit the parent class
Function play (){
Echo "{$ this-> name} I am playing a game;
}
}
$ My_father = new father ("DAD"); // create a parent class object
$ My_father-> work ();
$ My_son = new son ("son ");
$ My_son-> work ();
$ My_son-> play ();
Resolution: in the parent father class, we define general attributes and methods, and then define sub-classes. You may find that there is no constructor or Destructor in the subclass. because the subclass inherits all the methods of the parent class, you can call $ my_son-> work (); this is the inheritance of PHP classes. In addition, you must note that PHP cannot inherit from multiple layers, for example, class A extends B extends C. such inheritance is invalid in PHP. in PHP, there is only one inheritance, and there cannot be more inheritance, other methods are required to "implement" multiple inheritance in disguise.
For more new experiences, please try out the various Web development tools (including PHPIDE) under JetBrains ). What's more, I have a discount on flash sales and free iPhone sales? 6. iPad? Air and so on!