PHP class package and inheritance in detail, PHP package in detail
Packaging
Encapsulating member methods and member properties into a class, hiding the details of the properties and methods implementation, and restricting access to the members of the class, such as public, protected, private, and so on, the data is protected internally, only by the authorized member method, and the members are encapsulated as much as possible.
Public : The method or property can be accessed under any scope, and is the default, and it will be public if no access modifier is specified for a property or method.
Protected: This class and subclass can be accessed, and external objects cannot be called.
Private: can only be accessed in this class, and it is not possible for subclasses and external objects to be called. A method or property that is marked by private can be redefined in an inherited class, and each class can see only its own private methods that it defines.
These three modifiers should be sorted from large to small in terms of scope: Public→protected→private is scoped, because the class encapsulates properties and methods, which determines the "visibility" of the data, and In this way we can not arbitrarily modify the properties and methods defined on the outside of the class and only be able to invoke, this is the benefits of encapsulation, but also improve security.
We give the code example:
Class myclass{public $public = "public"; The public property protected $protected = "protected";//protected property Private $private = "private"; Private attribute function Say_hello () { //public property //Just an example, add content yourself } $obj =new myClass (); echo $obj->public; Echo $obj->protected;
By running the example above we get a "public", but when you remove//echo $obj->private;, you get the following error:
Fatal error:cannot Access protected property MyClass:: $protected the e:apachehtdocsexamplefile.php on line 13.
We can see that we are not able to access the class's property definitions at will, and we do not know in the "outside" that there are members in this class, because these members may not be available for other classes. Of course, if we have to access or modify properties defined as "private," you can also use the system methods provided by PHP: _get () and _set ().
Inherited
You can inherit a class and have another member property and method that already exists for the class, which is called the parent class or base class, and the inheriting class is a subclass. The inheritance relationship is implemented through the extends keyword. In layman's terms, there has to be a "root cause" for inheritance, which you may imagine as having a son or daughter, who will get some "things (properties and methods)" from you, so that your "offspring" is holding all of your (root) traits.
Generate a "root" class (parent or base class)
Syntax: Class father{
}
Produce "Descendants" (subclass)
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'm working;} function __destruct () {} //destructor} class Son extends father{ //Inherit parent function play () { echo ' {$this- >name} I'm playing a game; } } $my _father=new father ("Papa"); Create a parent class object $my _father->work (); $my _son=new son ("sons"); $my _son->work ();
Parsing: In the parent class father, we define the general properties and methods, and then define the subclasses. You may find that there are no constructors and destructors within subclasses, because subclasses are all methods that inherit the parent class, so you can call $my_son->work (); This is the inheritance of the PHP class. Also note: PHP can not be multi-layered inheritance, such as: Class A extends B extends C, such inheritance in PHP is invalid, only single inheritance in PHP, can not inherit more, need other ways to "implement" multiple inheritance.
The above is about the PHP class packaging and inheritance learning content, I hope that everyone's learning has helped.
http://www.bkjia.com/PHPjc/1058166.html www.bkjia.com true http://www.bkjia.com/PHPjc/1058166.html techarticle PHP class encapsulation and inheritance in detail, the PHP encapsulation package in the package of member methods and member properties into the class, hidden properties and methods to implement the details, through public, protected, private and other limits ...