Home  >  Article  >  Backend Development  >  Object-oriented PHP_PHP tutorial

Object-oriented PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-14 10:07:45758browse

Many languages ​​are inherently object-oriented, but PHP took several years to introduce such functionality. Many people believe that previous attempts to add object-oriented features to the language have failed. Although version 4 provides very basic object-oriented programming (OOP) concepts, there are some shortcomings, including:

· Informal object reference method
· The scope of fields and methods cannot be set (public, private, protected, abstract).
· No named constructor or standard convention
· Lack of destructor of object.
· Lack of object copy feature.
· Lack of support for interfaces.
Fortunately, PHP5 eliminates all the above shortcomings, substantially improves the original implementation, and adds many new OOP features.
Benefits of OOP
The birth of object-oriented programming is a major change in the development paradigm. The focus of programming returns from the logic of the application to its data. In other words, OOP shifts the focus from the procedural events of programming to the final construction. The real body of the model. This brings applications closer to the real world around us.
Encapsulation
Programmers generally have a strong sense of curiosity. We like to take things apart and understand how all the small parts inside work together. While spiritually satisfying, a deep understanding of the inner workings of things is not necessary. For example, millions of people use computers every day, but few truly understand how they work. Likewise, cars, televisions As is the case with many commonplace things, the internal structure can be ignored by using interfaces. For example, you know that turning a dial on your radio changes the channel, but you may not know that you are actually telling the radio to listen for signals transmitted on a specific frequency. This is done through a demodulator, even if you don't understand the process. , nor does it affect the use of the radio, because the interface hides these details, separating the user from the inner workings of the actual application through a well-known interface, an approach called encapsulation.
Object-oriented programming takes the concept of hiding the inner workings of an application a step further by establishing well-defined interfaces that are accessible to every application component. Developers with an OOP mindset do not get bogged down in a lot of details, but instead design components that interact with well-defined interfaces, called objects, that are tightly coupled or coupled independently of other components. Objects are created through a template called a class, which is used to define the data and behavior that an entity should have. This approach has the following advantages:
· Developers can modify the application's implementation without affecting the object's users, because users only interact with the object through the object's interface.
· Will reduce the possibility of user error because there is control over the user's interaction with the application.
Inheritance www.2cto.com
Many things (including people) in the environment around us can be modeled using a well-defined set of rules. Let's look at the concept of employees. All employees have a common set of properties, such as name, employee number, and salary. However, there are many non-scold types of employees such as clerks, supervisors, cashiers, and CEOs. Each type of employee possesses a superset of the properties defined in the general employee definition. In object-oriented terms, these employee types inherit the general employee definition, including all properties and behaviors in this definition. Next, each specific employee class can also be inherited by another more specific class. , for example: the "staff" type can be inherited by day shift staff and night shift staff. Both day shift staff and night shift staff will inherit all properties in the employee definition and staff definition. Based on this concept, you can create another "person" class and make the "employee" class a subclass of the "person" class. As a result, the employee class and all its derived classes (clerk, cashier, CEO, etc.) will immediately inherit All properties and behaviors defined in the "human" class.
Object-oriented development methods are based on the concept of inheritance. This strategy improves the reusability of code because it enables one to use well-designed classes in multiple applications [so-called well-designed (well- designed), meaning that these classes are abstract enough to be reusable].
Polymorphism
Polymorphism is a term from Greek, and its original meaning is "having multiple forms". Simply put, polymorphism refers to OOP's ability to redefine or change the nature or behavior of a class based on the context in which the class is used.
Still using an example to explain, suppose there is a "check-in" and behavior in the definition of the employee class. For staff employees, this behavior may specifically be to use a clock to clock in. For other types of employees, for example, " "Programmer" sign-in may be done over the network. Although both classes inherit this behavior from the employee class, the specific implementation depends on the context in which "check-in" is implemented. This is the power of polymorphism

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477844.htmlTechArticleMany languages ​​are inherently object-oriented, and it took PHP several years to introduce such functionality. Many people believe that previous attempts to add object-oriented features to the language have failed. All...
Statement:
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