In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "what are the object-oriented questions in the php interview?" interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the object-oriented questions in the php interview?"
The content modules involved in the structure of the whole object-oriented article are:
What's the difference between object-oriented and process-oriented?
Second, what are the characteristics of object-oriented?
What are constructors and destructors?
4. What are the scope of object-oriented?
What are the magic methods in PHP?
What is object cloning?
What is the difference between this, self and parent?
What is the difference and relationship between abstract classes and interfaces?
9. Explanation of PHP object-oriented regular examination questions
The object-oriented content of PHP will be divided into three articles to explain the full block content, the first one will mainly explain one to four points, the second will focus on five to eight, and the third will focus on the ninth point.
The following text comes from the book "PHP programmer interview written examination Book". If you reprint it, please keep the source:
What's the difference between object-oriented and process-oriented?
Object-oriented is one of the mainstream methods of software development, it is to put the data and the operation methods of the data together, as an interdependent whole, that is, objects. Abstract the commonness of similar objects, that is, the class, most of the data in the class can only be processed by the methods of this class. Classes communicate with the outside world through a simple external interface, and objects communicate with each other through messages. The flow of the program is determined by the user in use. For example, from an abstract point of view, human beings have some special names such as height, weight, age, blood type, etc. human beings can work, walk upright, eat, create tools with their own minds, and so on. Human beings are only an abstract concept, it is a non-existent entity, but all objects with the attributes and methods of this group of human beings are called people, and this object person is an actual entity. Everyone is an object of this group of people.
Process-oriented is an event-centered development method, which is executed sequentially from top to bottom and refined step by step. Its program structure is divided into several basic modules according to function, and these modules form a tree structure. The relationship between each module is also relatively simple and functionally independent. Each module is generally composed of three basic structures: sequence, selection and loop. The specific method of modular implementation is to use subroutines, and the program flow has been decided when the program is written. For example, Gobang, the process-oriented design idea is the first step to analyze the problem: the first step is to start the game; the second step is to sunspot first; the third step is to draw the picture; the fourth step is to judge whether to win or lose; the fifth step is to draw the picture; the sixth step is to draw the picture; the seventh step is to judge whether to win or lose; the eighth step is to return to step two; and the ninth step is to output the final result. Implementing each of the above steps with separate functions is a process-oriented development approach.
Specifically, the two are mainly different in the following aspects.
1) the starting point is different. Object-oriented is to deal with the problems of the objective world in accordance with the conventional way of thinking, emphasizing that the essentials of the problem domain are directly mapped to the object and the interface between objects. However, the process-oriented method is not. It emphasizes the abstraction and modularization of the process. It constructs or deals with the problems of the objective world with the process as the center.
2) the hierarchical logical relations are different. On the other hand, the object-oriented method uses computer logic to simulate the physical existence in the objective world, takes the collection class of objects as the basic unit to deal with the problem, and makes the computer world closer to the objective world as much as possible, so as to make the problem deal with more clearly and directly. the object-oriented method uses the hierarchical structure of classes to reflect the inheritance and development between classes. The basic unit of the process-oriented method to deal with the problem is the module that can express the process clearly and accurately. The hierarchical structure of the module is used to summarize the relationship and function between the modules or modules, and the problems in the objective world are abstracted into a process that the computer can deal with.
3) the way of data processing is different from that of control program. The object-oriented method encapsulates the data and the corresponding code into a whole, in principle, other objects can not modify their data directly, that is, the modification of the object can only be completed by its own member functions. In the way of the control program, the program is activated and run by "event-driven". On the other hand, the process-oriented method processes the data directly through the program, and the processing result can be displayed after the processing is finished. in the way of the control program, it is called or returned to the program according to the design, and can not be navigated freely. there is control and being controlled, called and called between the modules.
4) the way of analysis and design is different from that of transcoding. The object-oriented method runs through the analysis, design and coding of the software life cycle is a smooth process, from analysis to design to coding is a consistent model representation, that is, to achieve a seamless connection. On the other hand, the process-oriented method emphasizes the transformation between analysis, design and coding according to rules, and realizes a kind of stitched connection through the analysis, design and coding of software life cycle.
Second, what are the characteristics of object-oriented?
The main features of object-oriented are abstraction, inheritance, encapsulation and polymorphism.
1) abstraction. Abstraction is to ignore aspects of a topic that have nothing to do with the current goal in order to pay more attention to the aspects related to the current goal. The abstraction does not intend to understand all the problems, but only chooses some of them, without some details for the time being. Abstraction includes two aspects, one is process abstraction, the other is data abstraction.
2) inheritance. Inheritance is a hierarchical model that joins classes and allows and encourages class reuse. It provides a way to clearly express commonalities. A new class of an object can be derived from an existing class, a process called class inheritance. The new class inherits the characteristics of the original class, which is called the derived class (subclass) of the original class, and the original class is called the base class (parent class) of the new class. A derived class can inherit methods and instance variables from its base class, and subclasses can modify or add new methods to better suit their special needs.
3) encapsulation. Encapsulation refers to abstracting objective things into classes, each of which protects its own data and methods. Classes can only let trusted classes or objects manipulate their own data and methods to hide untrusted information.
4) Polymorphism. Polymorphism means that objects of different classes are allowed to respond to the same message. Polymorphism includes parameterized polymorphism and inclusion polymorphism. Polymorphic language has the advantages of flexibility, abstraction, behavior sharing and code sharing, which solves the problem of the same name of application function very well.
What are constructors and destructors?
1. Constructor
In previous versions of PHP5, the name of the constructor had to be the same as the name of the class, while starting with PHP5, developers could define a method named _ _ construct as the constructor. The function of the constructor is to be called automatically when the class is instantiated, so it is mainly used to do some initialization work. One advantage of using _ _ construct as the constructor name is that when the class name is changed, there is no need to change the constructor name. It is declared in the form of
Void _ _ construct ([mixed $args [, $...]])
In the C++ language, the constructor of the subclass implicitly calls the parameterless constructor of the parent class. But in PHP, the constructor of the subclass does not implicitly call the constructor of the parent class, but requires the developer to explicitly call the constructor of the parent class through parent::__construct (). When a subclass does not define a constructor, it inherits the constructor of the parent class, but only if the constructor of the parent class cannot be defined as private. Examples of use are as follows:
The running result of the program is
Base constructor
Base constructor
Sub constructor
As you can see from the above explanation, there is an additional method of constructor definition starting with PHP5. In order to achieve compatibility between different versions of PHP code, if the _ _ construct () function is not found in the PHP5 class and does not inherit one from the parent class, then it will try to find an old-fashioned constructor (a function with the same name as the class). There is a risk with this compatible approach: when a class developed in previous versions of PHP5 has a method named _ _ construct () that is used for other purposes, the PHP5 class considers this to be a constructor and automatically executes this method when the class is instantiated.
Starting with PHP 5.3.3, methods with the same name as the class name are no longer used as constructors in the namespace. This change does not affect classes that are not in the namespace.
2. Destructor
The destructor, introduced in PHP5, does the opposite of the timing of the call and the constructor, and it executes automatically when the object is destroyed. The destructor _ _ destruct () is structured as follows:
Function _ _ destruct () {/ * initialization code of class * /}
It is important to note that the destructor is called automatically by the system, so it does not require parameters.
By default, the system only frees the memory occupied by the object's properties and does not destroy the resources requested inside the object (for example, opening files, creating database connections, and so on). Instead, the destructor executes code after using an object to clear these resources requested inside the object (close the file, disconnect from the database).
Similar to the constructor, if you want to call the parent class's destructor in a subclass, you need to explicitly call: parent::__destruct (). If the subclass does not define a destructor, it inherits the destructor of the parent class.
When the object is no longer referenced, the destructor is called. If you want to destroy an object explicitly, you can assign no value to the variable pointing to the object, usually assigning the variable to NULL or using the unset () function. The sample code is as follows:
4. What are the scope of object-oriented?
In PHP5, the properties or methods of a class are mainly scoped by public, protected, and private. The differences between them are as follows:
1) public (public type) represents the global, and can be accessed by internal, external, and subclasses of the class.
The default access permission is public, that is, if a method is not modified by public, protected, or private, its default scope is public.
2) protected (protected type) means protected, and only this class or subclass can access it.
In a subclass, you can access it through self::var or self::method, or you can call methods in the parent class through parent::method.
Methods or properties of type protected cannot be accessed through $obj- > var in the instantiated object of the class.
3) private (private type) means private, and can only be used within this class.
Properties or methods of this type can only be used in this class, and properties and methods of private types cannot be called in instances of this class, subclasses, or subclasses.
At this point, I believe you have a deeper understanding of "what are the object-oriented questions in the php interview?" 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.