Related Object Oriented Questions in php Interview

  • 2021-11-24 01:08:51
  • OfStack

PHP object-oriented often test knowledge points have the following 7 points, I will give a detailed introduction from the following points to help you better deal with PHP interview often test object-oriented knowledge points and test questions.

The content modules involved in the structure of the whole object-oriented article are:

1. What is the difference between object-oriented and process-oriented? 2. What are the characteristics of object-oriented? 3. What are constructors and destructors? 4. What are the object-oriented scopes? 5. What are the magic methods in PHP? 6. What is object cloning? 7. What is the difference between this, self and parent? 8. What are the differences and connections between abstract classes and interfaces? 9. Explanation of PHP object-oriented interview questions

The object-oriented content of PHP will be divided into three articles to explain the whole content. The first article mainly explains points 1 to 4, the second article mainly explains points 5 to 8, and the third article focuses on point 9.

The contents of the following texts are all from the book "PHP Programmer Interview Written Test Book". If reprinted, please keep the source:

1. What is the difference between object-oriented and process-oriented?

Object-oriented is one of the mainstream methods of software development. It puts data and its operation methods into one, as an interdependent whole, that is, objects. Abstracting the commonness of similar objects, that is, classes, most of the data in classes can only be processed by the methods of this class. Class through a simple external interface with the external relations between objects and objects through the message communication. The program flow is decided by the user in use. For example, from an abstract point of view, Human beings have one special name, such as height, weight, age and blood type. Human beings can work, walk upright, eat, and use their own minds to create tools. Human being is only an abstract concept, which is a nonexistent entity, but all objects with the attributes and methods of human being are called human beings, and this object is an actual entity, and everyone is an object of human being.

Process-oriented is an event-centered development method, Is executed sequentially from top to bottom, Gradually refine, Its program structure is divided into several basic modules according to functions, These modules form a tree structure, the relationship between each module is relatively simple, and their functions are relatively independent. Each module is generally composed of three basic structures: sequence, selection and cycle. The specific method of modularization is to use subroutines, and the program flow has been decided when writing programs. For example, 5 sub-chess, the process-oriented design idea is the first step to analyze the problem: the first step is to start the game; Step 2, sunspots go first; Step 3, drawing a picture; Step 4, judge whether to win or lose; Step 5, it's Bai Zi's turn; Step 6, drawing a picture; Step 7: Judge whether you win or lose; Step 8, return to step 2; Step 9, output the final result. Each step above is implemented with a separate function, which is a process-oriented development method.

Specifically, the two are mainly different in the following aspects.

1) Different starting points. Object-oriented method deals with the problems in the objective world in a conventional way of thinking, emphasizing that the essentials of the problem domain are directly mapped to the objects and the interfaces between them. However, process-oriented method is not, emphasizing the abstraction and modularization of the process, which constructs or deals with the problems in the objective world with the process as the center.

2) Different hierarchical logical relations. Object-oriented method uses computer logic to simulate the physical existence in the objective world, Taking the set class of objects as the basic unit of dealing with problems, the computer world is close to the objective world as much as possible, so as to make the problem handling clearer and more direct. The object-oriented method uses the hierarchical structure of classes to reflect the inheritance and development between classes. The basic unit of process-oriented method is the module that can express the process clearly and accurately. The relationship and function between modules are summarized by the hierarchical structure of modules, and the problems in the objective world are abstracted into processes that can be handled by computers.

3) The data processing mode is different from the control program mode. Object-oriented method encapsulates data and corresponding code into a whole. In principle, other objects can't modify their data directly, that is, the modification of objects can only be completed by their own member functions, and the control program is activated and run by "event-driven". The process-oriented method is to process data directly through programs, and the processing results can be displayed after processing. In the control program mode, it calls or returns programs according to design, and can not navigate freely. There are control and being controlled, call and being called among modules.

4) Analysis design is different from coding conversion mode. Object-oriented method is a smooth process between analysis, design and coding throughout the software life cycle. From analysis to design and then to coding, it is expressed by a uniform model, that is, it realizes a seamless connection. The process-oriented method emphasizes the transformation among analysis, design and coding according to rules, which runs through the analysis, design and coding of software life cycle and realizes a slit connection.

2. What are the characteristics of object-oriented?

The main characteristics of object-oriented are abstraction, inheritance, encapsulation and polymorphism.

1) Abstraction. Abstraction is to ignore those aspects of a topic that have nothing to do with the current goal, so as to pay more attention to the aspects related to the current goal. Abstraction is not intended to understand all the problems, but only to select one part of them and not to use some details for the time being. Abstraction includes two aspects, one is process abstraction and 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 express commonalities explicitly. A new class of an object can be derived from an existing class. This process is called class inheritance. The new class inherits the characteristics of the original class. The new class is called a derived class (subclass) of the original class, while 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 make them more appropriate for special needs.

3) Encapsulation. Encapsulation refers to abstracting objective things into classes, and each class protects its own data and methods. Classes can only let their own data and methods be operated by trusted classes or objects, and hide untrusted information.

4) Polymorphism. Polymorphism refers to allowing objects of different classes to respond to the same message. Polymorphism includes parameterized polymorphism and inclusion polymorphism. Polymorphism language has the advantages of flexibility, abstraction, behavior sharing and code sharing, and solves the problem of application function with the same name well.

3. What are constructors and destructors?

1. Constructor

Prior to PHP5, the name of the constructor must be the same as the name of the class, whereas from PHP5, developers can define a method named __construct as the constructor. The function of the constructor is that when the class is instantiated, it will be called automatically, so the constructor 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 a subclass implicitly calls the parameterless constructor of the parent class. However, 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, provided that the constructor of the parent class cannot be defined as private. Examples of use are as follows:


<?php  
  class BaseClass {
    function __construct() {
      print "Base constructor\n";
    }
  }
  class SubClass extends BaseClass {
    function __construct() {
      parent::__construct();
      print "Sub constructor\n";
    }
  }
  //  The parent class constructor is called 
  $obj = new BaseClass();
  // Call the subclass constructor, and the subclass constructor will call the parent class constructor 
  $obj = new SubClass();
?>

The running result of the program is

Base constructor
Base constructor
Sub constructor

From the above explanation, we can find that there is one more method of constructor definition from PHP5. For compatibility with different versions of PHP code, if the __construct () function is not found in the class of PHP5 and does not inherit one from the parent class, it will try to find the old-fashioned constructor (the function with the same name as the class). There is a risk with this compatible method: When a method named __construct () already exists in a class developed in versions prior to PHP5 but is used for other purposes, the PHP5 class will consider it a constructor and automatically execute the method when the class is instantiated.

Beginning with PHP 5.3. 3, a method with the same name as a class name is no longer used as a constructor in the namespace. This 1 change does not affect classes that are not in the namespace.

2. Destructor

The destructor, introduced in PHP5, is the opposite of call timing and constructor, and executes automatically when an object is destroyed. The structure of the destructor __destruct () is as follows:


function __destruct(){
  /*  Class initialization code */
}

It should be noted that the destructor is automatically called by the system, so it does not need parameters.

By default, the system only releases the memory occupied by object attributes, and does not destroy the resources applied inside the object (for example, opening files, creating connections to the database, etc.), but uses destructors to execute code after using 1 object to clear these resources applied inside the object (closing files, disconnecting from the database).
Like constructors, if you want to call the parent's destructor in a subclass, you need to explicitly call: parent:: __destruct (). If a 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 explicitly destroy an object, 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:


<?php 
  class des{
    function __destruct(){
      echo " The object is destroyed and the destructor is executed <br>";
    }
  }
  $p=new des(); /*  Instantiate class  */
  echo " Program start <br>";
  unset($p); /*  Destroy variables $p */
  echo " End of program ";
?>

4. What are the object-oriented scopes?

In PHP5, the properties or methods of a class mainly have public, protected, and private3 class scopes, which differ as follows:

1) public (public type) means that global, internal, external, and subclasses are accessible.

The default access right is public, which means that if a method is not decorated with public, protected, or private, its default scope is public.

2) protected (protected type) means protected, accessible only to this class or subclasses.

In the child class, you can access it through self:: var or self:: method, or you can call methods in the parent class through parent:: method.
In an instantiated object of a class, you cannot use $obj- > var to access methods or properties of type protected.

3) private (private type) means private and can only be used inside 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, subclasses or instances of subclasses of this class.


Related articles: