Encapsulation and inheritance in PHP

Source: Internet
Author: User

1. Encapsulation

1.1 what is encapsulation?

By accessing the modifier, the properties and methods in the class are privatized and provided with unique settings and read

The Set/get method is used to implement access control. This process is called encapsulation.

Note : Access control is implemented, not access denied. In other words, the mere privatization attribute is not encapsulated, and the corresponding Set/get method must be provided to control the user's operation using the method we provide.

What is the role of the 1.2 package?

① users only need to be concerned about what functions the class can provide, without needing to care about the implementation details of those features. (Encapsulation of methods)

② controls the user's data, prevents the setting of illegal data, and controls the data returned to the user. (Encapsulation of attributes)

1.3 How to implement encapsulation?

Encapsulation of the ① method

For some functions that only need to be used internally within the class and do not need to provide functionality externally, we can use the private keyword for privatization.

          Private function Formaname () {}           function ShowName () {              $this->formaname ();   The method of privatization can only be used within the class $this calls          }

Encapsulation + Set/get method for ② properties

          Private $age ;           function setage ($age) {              $this$age;          }           function Getage () {              return$this, age;          use properties:$ object Name - >setage (+);           Echo $ object Name ->getage ();

Encapsulation of ③ attributes + Magic method

          Private $age; function__set ($key,&value) {             $this-$key=$value; }          function__get ($key){              return $this-$key; Use properties:$ object Name->age=12;//when you set a private property, __set () is called automatically          $ object Name->age;//when a private property is read, __get () is called automatically

The Magic method used in 1.4 encapsulation

__set ($key, $value): The Magic method is called automatically when you set non-exposed properties in a class. and pass two parameters, $key--Set the property name $value--Set the property value, no return value.

__get ($key): The Magic method is called automatically when you read non-exposed properties in a class. and pass a parameter, $key--Read the property name, return value: The property value that you want to display.

__isset ($key): called automatically when a non-public property is detected outside the class using Isset ().

>>> external use isset () to detect non-public properties, touch others do not take, return false;

>>> So, we can use __isset () to return the test results internally.

          function __isset ($key) {              returnisset($this,$ Key);          }

__unset ($key): Automatically called when a private property is deleted outside the class using unset ().

        function __unset ($key) {            unset($this-$key );        }

2. Inheritance

2.1 What does inheritance mean?

Subclasses inherit the parent class, and the subclass has properties and methods that are not private to the parent class.

How does 2.2 implement inheritance?

Using the extends keyword for subclasses allows subclasses to inherit from the parent class.

Class Student extends Person ()Considerations for 2.3 inheritance

The ① subclass inherits only the private properties of the parent class and cannot inherit the private property.

After the ② subclass inherits the parent class, it is equivalent to copy the parent class non-private properties and methods directly to the subclass. You can use the $this-> call directly, no different from the properties and methods of the subclass itself!

③php only supports single inheritance, which means that a class can have only one parent class. However, PHP can inherit multiple layers, such as:

         class person{}          class extends person{}          class extends student{}          // Goodstudent inherits the student class, and it has all the non-private properties and methods of the student class and the person class. 

2.4 Method Overrides (method overrides)

Conditional ① Subclass Inherits Parent class

Conditional ② Subclasses overriding the existing name method of the parent class

These two conditions, called method overrides, are also called method overrides. After overwriting, the subclass calls the method, which invokes the method immediately after the subclass itself overrides.

If the subclass overrides the parent class method, how do I invoke the parent class's method with the same name in the child class?

     // Called with the parent class name     // Use the parent keyword to refer to the Parental class name

So, when the subclass inherits the parent class, the first step is to override the constructor of the parent class in the subclass. In the constructor of the subclass, the first step requires that the constructor of the parent class be called to assign the value.

         function __construct ($name,$age,$school) {        parent:: __construct ( $name,$age);           $this $school  ;     }

Encapsulation and inheritance in PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.