Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the concept of encapsulation in php

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what is the concept of encapsulation in php". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the concept of encapsulation in php"?

In php, encapsulation is to separate the use and implementation of a class, leaving only limited interfaces (methods) with external connections; developers who use this class only need to know how to use the class, regardless of how the class is implemented.

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

PHP is an object-oriented programming language.

Object-oriented is a kind of programming thought which accords with the habit of human thinking. There are different forms of things in real life, and there are all kinds of connections between these things. Using objects to map real things in a program uses the relationship of objects to describe the relationship between things. This idea is object-oriented.

Characteristics of object-oriented programming

Object-oriented programming has three characteristics: encapsulation, inheritance and polymorphism, which cater to the needs of code reusability, flexibility and expansibility in programming, and establish the position of object-oriented in programming.

1) Encapsulation

Encapsulation is to separate the use and implementation of a class, leaving only a limited number of interfaces (methods) with external connections. For developers who use this class, they only need to know how the class should be used, regardless of how the class is implemented. This allows developers to better focus on other things while avoiding the inconvenience of interdependence between programs.

For example, when using a computer, we do not need to take the computer apart to understand the specific use of each part of it, we only need to press the power key to start the computer, which reflects the benefits of packaging.

2) inheritance

Inheritance is that derived classes (subclasses) automatically inherit properties and methods from one or more base classes (parent classes) and can override or add new properties or methods. Inheriting this feature simplifies the creation of objects and classes and increases the reuse of code.

For example, you have defined class A, and then you are going to define class B, and there are many properties and methods in class B that are the same as class A, so you can inherit class A with class B. in this way, you no longer have to define the properties and methods that already exist in class An in class B. as a result, the efficiency of program development can be greatly improved.

Inheritance is divided into single inheritance and multiple inheritance. PHP currently only supports single inheritance, that is, a subclass has one and only one parent class.

3) Polymorphism

The state of an object is changeable. An object can have different states compared to another object of the same class, although they have the same properties and methods. In addition, a class can derive several subclasses, which not only retain some properties and methods of the parent object, but also define some new methods and properties, or even completely overwrite some existing methods in the parent class. Polymorphism enhances the flexibility and reusability of the software.

Encapsulation in php

Encapsulation is one of the object-oriented features of php, which encapsulates multiple reusable functions into a single class. Directly instantiate one of the methods of this class when in use to get the required data

If it is private methods and property values, the outside cannot be accessed, so it has a certain protective effect.

Object-oriented encapsulation example

Class A {public $name = 'Lao Wang'; / / protected $name = 'Lao Wang'; / / private $name = 'Lao Wang'; / / visit public function saya () {return $this- > name;}} / / instantiate object $b = new A / / public: external, family and self can access / / protected: family and oneself can access, external cannot access / / private: oneself can access, external and family cannot access echo 'external access:'. $b-> name.'

'; / / if it's private, you can't access echo' Family visit:'. $b-> sayb ().

'; echo' visit:'. $b-> saya ().

At this point, I believe you have a deeper understanding of "what is the concept of encapsulation in php". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report