Object-oriented PHP_PHP tutorial-php Tutorial

Source: Internet
Author: User
Object-oriented PHP. Many languages are object-oriented, and PHP has used a few years to introduce such features. Many people think that the previous attempts to add object-oriented functions to the language failed. Many languages are object-oriented, and PHP has been introducing such features for several years. Many people think that the previous attempts to add object-oriented functions to the language failed. Although Version 4 provides a very basic object-oriented programming (OOP) concept, there are still some shortcomings, including:

· Informal object reference methods

· The scope of fields and methods cannot be set (public, private, protected, and abstract ).

· No naming constructor and standard conventions

· The object destructor is missing.

· The object replication feature is missing.

· Lack of support for interfaces.

Fortunately, PHP5 eliminates all the shortcomings mentioned above, makes essential improvements to the initial implementation, and adds many new OOP features.

Benefits of OOP

The emergence of object-oriented programming is a major change in the development model. the focus of programming is to return to the data of the application logic. In other words, OOP shifts focus from procedural events of programming to the real body of final modeling. This makes applications closer to the real world around us.

Encapsulation

Programmers generally have a strong curiosity. We like to take things apart and learn how all the small parts work together. Although it can be mentally satisfied, it is not necessary to have a deep understanding of the internal working principles of things. for example, millions of people are using computers every day, but few people really know how it works. this is also true for cars, TVs, and many things that are common. they can ignore its internal structure by using interfaces. For example, if you know that the rotating button of the radio can be changed, but you may not know that it is actually telling the radio to listen to signals transmitted at a specific frequency. this is done through a modem, even if you do not understand this process, it does not affect the use of the radio, because the interface hides these details, using a well-known interface to separate the user from the internal working principle of the actual application, this method is called encapsulation.

Object-oriented programming is designed by establishing well-defined interfaces (well-defined interfaces can be accessed by each application component ), the concept of hiding the internal working principle of an application is further improved. Developers with OOP thinking will not fall into a lot of details, but will design couple that is closely integrated or coupled independently of other components) an object is an interactive component with well-defined interfaces ). An object is created through a template called a class. a class is used to define the data and behavior of an object. this method has the following advantages:

· Developers can modify the implementation of the application without affecting the object users, because users only interact with the object through the interface of the object.

· Reduce possible user errors because it controls the interaction between users and applications.

Inherit from www.2cto.com

In our environment, many things (including people) can be modeled using a set of well-defined rules. Let's look at the concept of employee. all employees share a group of common characteristics, such as name, employee number, and salary. However, many employees, such as employees, supervisors, cashiers, and CEOs, do not scold others. Each type of employee has a superset of the nature defined in the general employee definition. In object-oriented terms, these employee type inheritance (inherit) defines general employees, including all the properties and actions in this definition. Next, each specific employee class can also be inherited by another more explicit class. for example, the "employee" type can be inherited by the white shift staff and night shift staff, both the staff on duty and night shift will inherit all the nature of the staff definition and staff definition. Based on this concept, you can create another "person" class to make the "employee" Class A subclass of the "person" class. The result is, the employee class and all its derived classes (employees, cashiers, CEOs, etc.) will immediately inherit all the properties and actions defined in the "person" class.

The object-oriented development method is based on the concept of inheritance, which improves the reusability of code, because it enables people to use well-designed classes in multiple applications [well-designed], these classes are abstract enough to reuse them].

Polymorphism

Polymorphism (polymorphism) is a term from Greek. it was originally intended to have multiple forms ". In short, polymorphism means that OOP can redefine or change the nature or behavior of a class based on the context of the class used.

Let's explain it using examples. suppose there is a "sign-in" and behavior in the definition of the employee class. for the employee, this behavior may specifically use the clock to punch in, for its type of employees, for example, the "programmer" sign-in may be performed over the network, although both classes inherit from the employee class to this behavior, but the specific implementation depends on the context of implementing "sign-in", which is the strength of polymorphism.

Bytes. Many people think that the previous attempts to add object-oriented functions to the language failed. All...

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.