Hướng dẫn what is the use of this and parent keywords in php class? - việc sử dụng từ khóa này và từ khóa cha trong lớp php là gì?

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc is a reserved keyword in PHP that refers to the calling object. It is usually the object to which the method belongs, but possibly another object if the method is called statically from the context of a secondary object. This keyword is only applicable to internal methods.

    Bàn luận A simple program to show the use of $this in PHP.

    $ Đây là một từ khóa dành riêng trong PHP đề cập đến đối tượng gọi. Nó thường là đối tượng mà phương thức thuộc về, nhưng có thể là một đối tượng khác nếu phương thức được gọi là tĩnh từ bối cảnh của một đối tượng thứ cấp. Từ khóa này chỉ áp dụng cho các phương thức nội bộ.

    Ví dụ 1: Một chương trình đơn giản để hiển thị việc sử dụng $ này trong PHP.

    class simple{

        public $k = 9;

        

    value of before update: 9
    value of after update: 8
    
    9

    value of before update: 9
    value of after update: 8
    
    9

        public

    value of before update: 9
    value of after update: 8
    
    2
    value of before update: 9
    value of after update: 8
    
    3

    value of before update: 9
    value of after update: 8
    
    4
    value of before update: 9
    value of after update: 8
    
    5
    value of before update: 9
    value of after update: 8
    
    6
    value of before update: 9
    value of after update: 8
    
    7

    $this is defined (A) 
    $this is not defined. 
    $this is not defined. 
    $this is not defined.
    
    8

    Output:

    9

    $this is defined (A) 
    $this is not defined. 
    $this is not defined. 
    $this is not defined.
    
    1
    $this is defined (A) 
    $this is not defined. 
    $this is not defined. 
    $this is not defined.
    
    22____23
    $this is defined (A) 
    $this is not defined. 
    $this is not defined. 
    $this is not defined.
    
    4
    Unlike other reserved keywords used in the context of class like the static, parent, etc does not need to be declared with the dollar sign (‘$’). This is because in PHP $this is treated as a pseudo-variable.
    In PHP, this is declared like a variable declaration (with the ‘$’ sign) even though it is a reserved keyword. More specifically, $this is a special read-only variable that is not declared anywhere in the code and which represents a value that changes depending on the context of program execution.

    $this is defined (A) 
    $this is not defined. 
    $this is not defined. 
    $this is not defined.
    
    5
    $this is defined (A) 
    $this is not defined. 
    $this is not defined. 
    $this is not defined.
    
    1
    $this is defined (A) 
    $this is not defined. 
    $this is not defined. 
    $this is not defined.
    
    7
    A program that updates the value of a variable of a specific object using $this keyword.

    $ Điều này-một biến giả giả: Không giống như các từ khóa dành riêng khác được sử dụng trong bối cảnh của lớp như tĩnh, cha mẹ, v.v ... không cần phải được khai báo với dấu hiệu đô la (‘$,). Điều này là do trong PHP $, điều này được coi là một phép biến giả. Cụ thể hơn, $ Đây là một biến chỉ đọc đặc biệt không được khai báo ở bất cứ đâu trong mã và đại diện cho một giá trị thay đổi tùy thuộc vào bối cảnh thực hiện chương trình.

    Ví dụ 1: Một chương trình đơn giản để hiển thị việc sử dụng $ này trong PHP.

    class simple{

        public $k = 9;

        

    value of before update: 9
    value of after update: 8
    
    9

    class simple{

        public $k = 9;

        

    value of before update: 9
    value of after update: 8
    
    9

    value of before update: 9
    value of after update: 8
    
    9

        public

    value of before update: 9
    value of after update: 8
    
    2
    value of before update: 9
    value of after update: 8
    
    3

        4    5    6    7

    value of before update: 9
    value of after update: 8
    
    4
    value of before update: 9
    value of after update: 8
    
    5
    value of before update: 9
    value of after update: 8
    
    6
    value of before update: 9
    value of after update: 8
    
    7

    $this is defined (A) 
    $this is not defined. 
    $this is not defined. 
    $this is not defined.
    
    1public2

        4    5public5    7

    value of before update: 9
    value of after update: 8
    
    4
    value of before update: 9
    value of after update: 8
    
    5
    value of before update: 9
    value of after update: 8
    
    6
    value of before update: 9
    value of after update: 8
    
    7

    $this is defined (A) 
    $this is not defined. 
    $this is not defined. 
    $this is not defined.
    
    8

    Output:

    value of before update: 9
    value of after update: 8
    

    $this is defined (A) 
    $this is not defined. 
    $this is not defined. 
    $this is not defined.
    
    1
    $this is defined (A) 
    $this is not defined. 
    $this is not defined. 
    $this is not defined.
    
    22____23
    $this is defined (A) 
    $this is not defined. 
    $this is not defined. 
    $this is not defined.
    
    4

    $this is defined (A) 
    $this is not defined. 
    $this is not defined. 
    $this is not defined.
    
    5
    $this is defined (A) 
    $this is not defined. 
    $this is not defined. 
    $this is not defined.
    
    1
    $this is defined (A) 
    $this is not defined. 
    $this is not defined. 
    $this is not defined.
    
    7
    In this example, $this keyword becomes “not defined” when a non-static method is called in the context of a static one.

    $ Điều này-một biến giả giả: Không giống như các từ khóa dành riêng khác được sử dụng trong bối cảnh của lớp như tĩnh, cha mẹ, v.v ... không cần phải được khai báo với dấu hiệu đô la (‘$,). Điều này là do trong PHP $, điều này được coi là một phép biến giả. Cụ thể hơn, $ Đây là một biến chỉ đọc đặc biệt không được khai báo ở bất cứ đâu trong mã và đại diện cho một giá trị thay đổi tùy thuộc vào bối cảnh thực hiện chương trình.

    Ví dụ 2: Một chương trình cập nhật giá trị của một biến của một đối tượng cụ thể bằng cách sử dụng từ khóa này.

    class 1

        public

    value of before update: 9
    value of after update: 8
    
    2 9class0class1

    value of before update: 9
    value of after update: 8
    
    4
    value of before update: 9
    value of after update: 8
    
    6class4class0class6

    Kể từ Php 7.0.0, gọi một phương thức phi tĩnh một cách thống trị từ bối cảnh không tương thích dẫn đến $ Đây là một phương thức không xác định đối với phương thức. Gọi một phương pháp phi tĩnh một cách thống trị từ bối cảnh không tương thích đã được không được dùng để kể từ Php 5.6.0. Tính đến Php 7.0.0, việc gọi một phương pháp phi tĩnh đã được không dùng nữa (ngay cả khi được gọi từ bối cảnh tương thích). Trước Php 5.6.0, các cuộc gọi như vậy đã kích hoạt một thông báo nghiêm ngặt.

    Ví dụ 3: Trong ví dụ này, $ Từ khóa này trở thành không được xác định khi một phương thức không tĩnh được gọi trong bối cảnh của một phương thức tĩnh.

    class $k3

    value of before update: 9
    value of after update: 8
    
    4
    value of before update: 9
    value of after update: 8
    
    9

        

    value of before update: 9
    value of after update: 8
    
    9

    value of before update: 9
    value of after update: 8
    
    9

        

    value of before update: 9
    value of after update: 8
    
    2 $k6

    value of before update: 9
    value of after update: 8
    
    4$k8 $k9
    value of before update: 9
    value of after update: 8
    
    6= 9;1

    = 9;2

    $this is defined (A) 
    $this is not defined. 
    $this is not defined. 
    $this is not defined.
    
    5 = 9;4class6

        

    value of before update: 9
    value of after update: 8
    
    9

    value of before update: 9
    value of after update: 8
    
    9

    = 9;2

    $this is defined (A) 
    $this is not defined. 
    $this is not defined. 
    $this is not defined.
    
    5 = 9;8
    value of before update: 9
    value of after update: 8
    
    6    7

    value of before update: 9
    value of after update: 8
    
    28
    value of before update: 9
    value of after update: 8
    
    33

    value of before update: 9
    value of after update: 8
    
    24

    = 9;2

    $this is defined (A) 
    $this is not defined. 
    $this is not defined. 
    $this is not defined.
    
    5
    value of before update: 9
    value of after update: 8
    
    03class6

    value of before update: 9
    value of after update: 8
    
    35
    value of before update: 9
    value of after update: 8
    
    40

    value of before update: 9
    value of after update: 8
    
    41

    $this is defined (A) 
    $this is not defined. 
    $this is not defined. 
    $this is not defined.
    
    8

    Output:

    $this is defined (A) 
    $this is not defined. 
    $this is not defined. 
    $this is not defined.
    

    Sử dụng từ khóa cha là gì trong PHP?

    Phụ huynh :: Trình truy cập nó đề cập đến các phương thức và thuộc tính trong lớp cha từ phạm vi của một lớp con. Vì lớp con nên ghi đè các thuộc tính theo mặc định, người truy cập có thể đề cập rõ ràng đến thuộc tính của lớp cha. Nó cũng tiện dụng khi cả hai lớp có thuộc tính có cùng tên.It refers to methods and properties in a parent class from the scope of a child class. Since the child class should override the properties by default, the accessor can explicitly refer to the parent class's property. It is also handy when both classes have properties with the same name.

    Việc sử dụng từ khóa này trong PHP là gì?

    $ Đây là một từ khóa dành riêng trong PHP đề cập đến đối tượng gọi.Nó thường là đối tượng mà phương thức thuộc về, nhưng có thể là một đối tượng khác nếu phương thức được gọi là tĩnh từ bối cảnh của một đối tượng thứ cấp.Từ khóa này chỉ áp dụng cho các phương thức nội bộ.refers to the calling object. It is usually the object to which the method belongs, but possibly another object if the method is called statically from the context of a secondary object. This keyword is only applicable to internal methods.

    Mục đích của việc này là gì và mở rộng trong PHP là gì?

    Từ khóa mở rộng được sử dụng để lấy một lớp từ một lớp khác.Điều này được gọi là thừa kế.Một lớp có nguồn gốc có tất cả các thuộc tính công khai và được bảo vệ của lớp mà nó bắt nguồn từ.to derive a class from another class. This is called inheritance. A derived class has all of the public and protected properties of the class that it is derived from.

    Cha mẹ và bản thân trong PHP là gì?

    Nếu bạn đã ghi đè lên foo () trong lớp con, thì hãy tự :: foo () gọi phiên bản lớp con trong khi Parent :: foo () gọi phiên bản cha mẹ gốc.self::foo() calls the child class version while parent::foo() calls the original parent version.