Home > Backend Development > PHP Tutorial > Recommended resources for PHP video tutorials from getting started to mastering

Recommended resources for PHP video tutorials from getting started to mastering

黄舟
Release: 2023-03-15 16:40:01
Original
3095 people have browsed it

In the course "PHP from Getting Started to Mastery Video Tutorial", PHP (foreign name: PHP: Hypertext Preprocessor, Chinese name: "Hypertext Preprocessor") is a general open source scripting language. The syntax absorbs the characteristics of C language, Java and Perl, which is easy to learn and widely used. It is mainly suitable for the field of Web development. PHP's unique syntax mixes C, Java, Perl, and PHP's own syntax. It can execute dynamic web pages faster than CGI or Perl. Compared with other programming languages, dynamic pages made with PHP embed programs into HTML (an application under the Standard Universal Markup Language) document for execution, and the execution efficiency is much higher than CGI that completely generates HTML tags; PHP can also execute compiled code. Compilation can achieve encryption and optimize code running, making the code run faster.

Recommended resources for PHP video tutorials from getting started to mastering

Course playback address: http://www.php.cn/course/351.html

The teacher’s teaching style:

The lectures are friendly and natural, unpretentious, not pretentious, nor deliberately exaggerated, but talk eloquently and carefully, and the relationship between teachers and students is In an atmosphere of equality, collaboration, and harmony, silent emotional exchanges are carried out, and the desire and exploration of knowledge are integrated into simple and real teaching situations. Students gain knowledge through quiet thinking and silent approval

The more difficult points in this video are the three major characteristics of object-oriented programming:

1. Encapsulation

Encapsulation literally means packaging. The professional point is information hiding, which refers to the use of abstract data types to encapsulate data and data-based operations to form an indivisible independent entity. The data is protected within the abstract data type and internal details are hidden as much as possible. , only retaining some external interfaces to communicate with the outside world. Other objects in the system can only communicate and interact with this encapsulated object through authorized operations wrapped outside the data. That is to say, the user does not need to know the internal details of the object, but can access the object through the interface provided by the object.

For encapsulation, an object encapsulates its own properties and methods, so it can complete its own operations without relying on other objects. There are three major benefits of using encapsulation:

1. Good encapsulation can reduce coupling.

2. The structure inside the class can be modified freely.

3. Members can be more precisely controlled.

4. Hide information and implement details.

Encapsulation privatizes the properties of an object and provides some methods for properties that can be accessed by the outside world. If we don’t want to be accessed by outside methods, we don’t have to provide methods for outside access. But if a class does not provide methods for external access, then this class is meaningless.

public class Husband {  
      
    /* 
     * 对属性的封装 
     * 一个人的姓名、性别、年龄、妻子都是这个人的私有属性 
     */  
    private String name ;  
    private String sex ;  
    private int age ;  
    private Wife wife;  
      
    /* 
     * setter()、getter()是该对象对外开发的接口 
     */  
    public String getName() {  
        return name;  
    }  
  
    public void setName(String name) {  
        this.name = name;  
    }  
  
    public String getSex() {  
        return sex;  
    }  
  
    public void setSex(String sex) {  
        this.sex = sex;  
    }  
  
    public int getAge() {  
        return age;  
    }  
  
    public void setAge(int age) {  
        this.age = age;  
    }  
  
    public void setWife(Wife wife) {  
        this.wife = wife;  
    }  
}
Copy after login

Encapsulation allows us to easily modify the internal implementation of a class without modifying the client code that uses the class. You can have more precise control over member variables.

1. public void setAge(int age) {  
2.     if(age > 120){  
3.         System.out.println("ERROR:error age input....");    //提示错误信息  
4.     }else{  
5.         this.age = age;  
6.     }  
7. }
Copy after login
. public String getSexName() {  
2.         if("0".equals(sex)){  
3.             sexName = "女";  
4.         }  
5.         else if("1".equals(sex)){  
6.             sexName = "男";  
7.         }  
8.         else{  
9.             sexName = "人妖";  
10.         }  
11.         return sexName;  
12.     }
Copy after login

2. Inheritance

2.1 Inheritance Overview

Inheritance is a technology that uses the definition of an existing class as a basis to create a new class. The definition of a new class can add new Data or new functions can also use the functions of the parent class, but they cannot selectively inherit the parent class. By using inheritance, we can reuse previous code very conveniently, which can greatly improve development efficiency.

Inheritance describes the "is-a" relationship. If there are two objects A and B, if it can be described as "A is B", it can mean that A inherits B, and B is inherited. The successor is called the parent class or super class, and the successor is called the subclass or derived class.

In fact, the successor is a specialization of the inheritor. In addition to having the characteristics of the inheritor, it also has its own unique characteristics. For example, cats can catch mice, climb trees, and other characteristics that other animals do not have. At the same time, in the inheritance relationship, the inheritor can completely replace the inherited, but not vice versa. For example, we can say that a cat is an animal, but we cannot say that the animal is a cat. This is actually the reason why we call this "upward transformation". ".

It is true that inheritance defines how classes relate to each other and share characteristics. For several identical or familiar classes, we can abstract their common behaviors or attributes and define them as a parent class or super class, and then use these classes to inherit the parent class. They can not only have the properties and methods of the parent class You can also define your own unique properties or methods.

At the same time, you need to remember three sentences when using inheritance:

1. The subclass has the non-private properties and methods of the parent class.

2. Subclasses can have their own attributes and methods, that is, subclasses can extend parent classes.

3. Subclasses can implement the methods of the parent class in their own way. (Introduced later).

These three things are indispensable when learning inheritance: constructor, protected keyword, and upward transformation

2.2 Constructor

Through the previous we know that subclasses can inherit the parent class In addition to those private properties and methods, there is another one that cannot be inherited by subclasses---the constructor. As for the constructor, it can only be called and cannot be inherited. To call the constructor of the parent class, we can use super().

The construction process spreads "outward" from the parent class, that is, starting from the parent class and completing the construction step by step to the subclasses. And we did not explicitly reference the constructor of the parent class. This is the cleverness of Java: the compiler will call the constructor of the parent class for the subclass by default.

However, this default call to the constructor of the parent class is prerequisite: the parent class has a default constructor. If the parent class does not have a default constructor, we must explicitly use super() to call the parent class constructor, otherwise the compiler will report an error: A constructor that conforms to the form of the parent class cannot be found.

For subclasses, the correct initialization of its constructor is very important, and if and only if there is only one method that can ensure this: calling the parent class constructor in the constructor to complete the initialization, and the parent The class constructor has all the knowledge and capabilities needed to perform initialization of the parent class.

For inheritance, the subclass will call the parent class's constructor by default. However, if there is no default parent class constructor, the subclass must explicitly specify the parent class's constructor, and it must be in the child class. The first thing done in the class constructor (the first line of code).

2.3 protected keyword

The private access modifier is the best choice for encapsulation, but this is only based on an ideal world. Sometimes we need such a requirement: We need Hide certain things as much as possible from the world, but still allow members of subclasses to access them. At this time you need to use protected.

For protected, it indicates that as far as the class user is concerned, it is private, but for any subclass that inherits this class or any other class located in the same package, it is accessible.

2.4 Upward Transformation

In the inheritance above, we talked about inheritance as a mutual relationship of is-a. Cat inherits from animals, so we can say that cats are animals, or that cats are animals. kind of. To think of cats as animals in this way is to transform upward.

3. Polymorphism

3.1 Overview of Polymorphism

The so-called polymorphism refers to the specific type pointed to by the reference variable defined in the program and the method emitted through the reference variable The call is not determined during programming, but is determined during the running of the program. That is, which class instance object a reference variable will point to. The method call issued by the reference variable must be implemented in which class. It can be decided while the program is running. Because the specific class is determined only when the program is running, the reference variable can be bound to various class implementations without modifying the source program code, causing the specific method called by the reference to change accordingly, that is, it does not need to be modified. The program code can change the specific code bound to the program when it is running, allowing the program to select multiple running states. This is polymorphism.

So we can summarize polymorphism as follows:

Since the parent class reference pointing to the subclass is upward transformed, it can only access the methods and properties owned by the parent class, and for the subclass For a method that exists in the parent class but does not exist in the parent class, the reference cannot be used, even if the method is overloaded. If a subclass overrides some methods in the parent class, when calling these methods, it must use the methods defined in the subclass (dynamic connection, dynamic calling).

For object-oriented, polymorphism is divided into compile-time polymorphism and run-time polymorphism. Among them, editing-time polymorphism is static and mainly refers to method overloading. It distinguishes different functions based on different parameter lists. After editing, it will become two different functions. There is no polymorphism at runtime. . Runtime polymorphism is dynamic and is achieved through dynamic binding, which is what we call polymorphism.

3.2 Conditions for the realization of polymorphism

I mentioned at the beginning that inheritance prepares for the realization of polymorphism. The subclass Child inherits the parent class Father. We can write a parent class type reference pointing to the subclass. This reference can handle either the parent class Father object or the subclass Child object. When the same message is sent to the subclass or the parent class When an object is used, the object will perform different behaviors according to the reference it belongs to. This is polymorphism. That is, polymorphism means that the same message causes different classes to respond differently.

There are three necessary conditions for Java to achieve polymorphism: inheritance, rewriting, and upward transformation.

Inheritance: In polymorphism, there must be subclasses and parent classes with inheritance relationships.

Rewriting: The subclass redefines certain methods in the parent class, and when these methods are called, the methods of the subclass will be called.

Upward transformation: In polymorphism, the reference of the subclass needs to be assigned to the parent class object. Only in this way can the reference have the ability to call the methods of the parent class and the methods of the subclass.

Only when the above three conditions are met, can we use unified logic to implement code to process different objects in the same inheritance structure, so as to perform different behaviors.

For Java, its polymorphic implementation mechanism follows one principle: when a superclass object reference variable refers to a subclass object, the type of the referenced object rather than the type of the reference variable determines whose member is called. method, but the called method must be defined in the super class, that is, a method overridden by the subclass.

3.3 Implementation form

There are two forms to achieve polymorphism in Java: inheritance and interface.

3.2.1. Polymorphism based on inheritance

The implementation mechanism based on inheritance is mainly reflected in the rewriting of certain methods by the parent class and one or more subclasses that inherit the parent class. , multiple subclasses overriding the same method can exhibit different behaviors.

Polymorphism implemented based on inheritance can be summarized as follows: For a parent class type that refers to a subclass, when processing the reference, it applies to all subclasses that inherit the parent class. Different subclass objects, for The implementation of the method is also different, and the behavior produced by performing the same action is also different.

If the parent class is an abstract class, then the subclass must implement all abstract methods in the parent class. In this way, all subclasses of the parent class must have a unified external interface, but their internal specific implementations can vary. different. In this way, we can use the unified interface provided by the top-level class to handle the methods at this level.

3.2.2. Polymorphism based on interface implementation

Inheritance is reflected by several different subclasses that override the same method of the parent class. Then it can be achieved by implementing the interface and It is embodied by several different classes covering the same method in the interface.

In the polymorphism of the interface, the reference pointing to the interface must be an instance program that specifies a class that implements the interface. At runtime, the corresponding method is executed according to the actual type of the object reference.

Inheritance is single inheritance, which can only provide a consistent service interface for a group of related classes. However, interfaces can have multiple inheritance and multiple implementations. They can be combined and expanded using a set of related or unrelated interfaces, and can provide consistent service interfaces to the outside world. So it has better flexibility than inheritance.

The above is the detailed content of Recommended resources for PHP video tutorials from getting started to mastering. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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