In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the "PHP class encapsulation and inheritance detailed explanation", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "PHP class encapsulation and inheritance detailed explanation"!
Encapsulation
Encapsulate the member methods and member attributes into the class, hide the details of the properties and method implementation, limit the access rights of the class members through public, protected, private and so on, the data is protected internally, only through the authorized member methods can operate, encapsulate the members as much as possible.
Public: a method or property can be accessed in any scope, and by default, if no access modifier is specified for a property or method, it will be public.
Protected: this class and subclasses can be accessed, but external objects cannot be called.
Private: can only be accessed in this class, and cannot be called by subclasses or external objects. Private-tagged methods or attributes can be redefined in inherited classes, and each class can only see its own defined private methods.
These three modifiers should be sorted from large to small in terms of scope: public → protected → private is said to be in scope because the class encapsulates some properties and methods, and this encapsulation determines the "visibility" of the data, so that we can not arbitrarily modify the defined properties and methods outside the class, but can only be called, which is the benefit of encapsulation, but also improves security.
Let's give a code example:
Class myClass {public $public= "Public"; / / public attribute protected $protected= "Protected"; / / protected attribute private $private= "Private"; / / private attribute function say_Hello () {/ / public attribute / / just for example, add content} $obj=new myClass (); echo $obj- > public; / / echo $obj- > protected; / / echo $obj- > private
We get a "Public" by running the above example, but when you remove the comment / / echo $obj- > private;, you get the following error:
Fatal error: Cannot access protected property myClass::$protected in E:apachehtdocsexamplefile.php on line 13 .
We can see that we can't access the property definition of the class at will. We don't know which members there are in this class "outside", because these members may not be available to other classes. Of course, if we have to access or modify properties defined as "private", we can also use the system methods provided by PHP: _ get () and _ set ().
Inherit
You can make one class inherit and have the member properties and methods of another existing class, the inherited class is called the parent class or the base class, and the inherited class is the subclass. Inheritance relationships are implemented through the extends keyword. In popular terms, if you want to inherit, you must have a "root". You may imagine that you will have a son or daughter, and they will get some "things (attributes and methods)" from you. In this way, your "offspring" hold all the characteristics of you.
Generate a "root" class (parent or base class)
Syntax: class father {
}
Produce "descendants" (subclasses)
Syntax: class son extends father {
}
The PHP extends class inherits the sample code:
Class father {protected $name; function _ construct ($name) {/ / Constructor $this- > name=$name;} function work () {echo "{$this- > name} I am working;} function _ destruct () {} / / destructor} class son extends father {/ / inherits parent function play () {echo" {$this- > name} I am playing games;}} $my_father=new father ("Dad") / / create parent object $my_father- > work (); $my_son=new son ("son"); $my_son- > work (); $my_son- > play ()
Parsing: in the parent class father, we define general properties and methods, and then define subclasses. You may find that there are no constructors and destructors in the subclass, because the subclass inherits all the methods of the parent class, so you can call $my_son- > work (); this is the inheritance of the PHP class. In addition, it should be noted that PHP cannot inherit at multiple levels, such as class An extends B extends C, which is invalid in PHP. In PHP, there is only single inheritance, not more inheritance. Other methods are needed to "realize" multi-inheritance in disguise.
Thank you for your reading, the above is the "PHP class encapsulation and inheritance detailed explanation" content, after the study of this article, I believe you have a deeper understanding of the PHP class encapsulation and inheritance detailed explanation of this problem, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.