Home > Backend Development > PHP Tutorial > Introduction to PHP Object-Oriented Programming_PHP Tutorial

Introduction to PHP Object-Oriented Programming_PHP Tutorial

WBOY
Release: 2016-07-15 13:25:02
Original
1015 people have browsed it

Object-oriented programming is designed to provide solutions for large-scale software projects, especially projects involving multiple people. When the source code grows to 10,000 lines or more, every change may lead to inaccuracies. A side effect of hope. This happens when modules form secret alliances, like in Europe before World War I.

Note: refers to the degree of correlation between modules Too high, too interdependent. Changing one module requires that other modules must also be changed.

Imagine if a module that handles logins allows a credit card processing module to share its database Connection. Of course, the starting point is good, saving the cost of another database connection. However, sometimes, the login processing module changes the name of one of the variables, which may sever the agreement between the two, causing processing errors in the credit card module, and then Causes the module that processes invoices to fail. Soon, all unrelated modules in the system may fail.

Therefore, I think it is a bit dramatic that most programmers are grateful for coupling and encapsulation. . Coupling is a measure of the degree of dependence between two modules. The less coupling, the better. We want to be able to take a module from an existing project and use it in a new project.

We also want to Large-scale changes within a module without worrying about the impact on other modules. The principle of encapsulation can provide this solution. Modules are regarded as relatively independent, and data communication between modules is carried out through interfaces. Modules do not pass each other's Variable names to spy on another module that politely sends requests through functions.

Encapsulation is a principle you can use in any programming language. In PHP and many procedural-oriented languages, being lazy is It's tempting. There's nothing stopping you from building a hypothetical WEB through modules. Object-oriented programming is a way for programmers to not violate the principle of encapsulation.

In object-oriented programming, modules are Organized into objects. These objects have methods and attributes. From an abstract point of view, methods are the actions of an object, and attributes are the characteristics of the object. From a programming point of view, methods are functions and attributes are variables. . In an ideal object-oriented system, each part is an object. The system is composed of objects and the relationships between objects through methods.

A class defines the properties of the object. If you When baking a set of cookie objects, the class will be the cookie maker. The properties and methods of the class are the members that are called. One can express this by saying data members or method members.

Each The language provides different ways to access objects. PHP borrows concepts from C++ and provides a data type to contain functions and variables under an identifier. When PHP was originally designed, and even when PHP3 was developed, PHP was not intended to provide the ability to develop large projects with more than 100,000 lines of code. With the development of PHP and Zend Engine, it has become possible to develop large projects, but no matter how big your project is, writing your scripts in classes will allow code reuse. This is a good idea, especially if you are willing to share your code with others.

The idea of ​​objects is one of the most exciting concepts in computer science. It's hard to master it at first, but I can guarantee that once you do, thinking with its mind will feel very natural.

Object Model of PHP5

PHP5 has a single inheritance, restricted access, and overloadable object model. "Inheritance" will be discussed in detail later in this chapter ”, including the parent-child relationship between classes. In addition, PHP supports restricted access to properties and methods. You can declare members as private, disallowing access from outside classes. Finally, PHP allows a subclass to be accessed from its parent class. Overloading members.

Note:There is no private in PHP4, only public.private is very beneficial for better implementation of encapsulation.

PHP5’s object model treats objects as Unlike any other data type, objects are passed by reference. PHP does not require you to explicitly pass and return objects by reference. The handle-based object model will be explained in detail at the end of this chapter. It is the most important in PHP5 New features.

With a more direct object model, the handle-based system has additional advantages: increased efficiency, less memory usage, and greater flexibility.

In PHP In previous versions, the script copied the object by default. Now PHP5 only moves the handle, which takes less time. The improvement in script execution efficiency is due to the avoidance of unnecessary copying. While the object system brings complexity, it also brings Gains in execution efficiency. At the same time, reducing copying means occupying less memory and leaving more memory for other operations, which also improves efficiency.

Note: is based on Handle means that two objects can point to the same memory, which not only reduces the copy action, but also reduces the memory usage.

Zand engine 2 has greater flexibility. An exciting development is to allow Destruction - Execute a class method before the object is destroyed. This is also good for utilizing memory, allowing PHP to clearly know when there are no references to the object and allocate the vacated memory to other uses.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446723.htmlTechArticleObject-oriented programming is designed to provide solutions for large-scale software projects, especially projects where multiple people collaborate. When the source When the code grows to 10,000 lines or more, every change may lead to...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template