Từ khóa trong PHP là gì?

$this 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à đối tượng khác nếu phương thức được gọi tĩnh từ ngữ cảnh của đối tượng phụ. Từ khóa này chỉ áp dụng cho các phương pháp nội bộ

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




class simple{

 

    public $k

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

 

    public

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_______0_______7
value of before update: 9
value of after update: 8
8
value of before update: 9
value of after update: 8
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.
1

 

$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_______7_______6
$this is defined (A) 
$this is not defined. 
$this is not defined. 
$this is not defined.
7

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

 

2

đầu ra

9

$this – một biến giả. Không giống như các từ khóa dành riêng khác được sử dụng trong ngữ cảnh của lớp như tĩnh, cha mẹ, v.v. không cần phải khai báo bằng ký hiệu đô la ('$'). Điều này là do trong PHP $this được coi là một biến giả
Trong PHP, điều này được khai báo giống như khai báo biến (có dấu ‘$’) mặc dù nó là từ khóa dành riêng. Cụ thể hơn, $this là một biến chỉ đọc đặc biệt không được khai báo ở bất kỳ đâu trong mã và đại diện cho một giá trị thay đổi tùy thuộc vào ngữ cảnh thực thi 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 từ khóa $this




class 5

 

    public $k

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

 

    44_______

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

value of before update: 9
value of after update: 8
6_______0_______8simple{0class6simple{2

    

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

 

    public

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_______0_______7
value of before update: 9
value of after update: 8
8
value of before update: 9
value of after update: 8
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.
1

 

$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_______7_______6
$this is defined (A) 
$this is not defined. 
$this is not defined. 
$this is not defined.
7

    2_______43_______3_______43_______4    5

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

 

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

    2_______43_______3_______44_______4    5

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

2

đầu ra

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

Kể từ PHP 7. 0. 0, việc gọi tĩnh một phương thức không tĩnh từ ngữ cảnh không tương thích dẫn đến $this là "không xác định" đối với phương thức. Việc gọi một phương thức không tĩnh một cách tĩnh từ ngữ cảnh không tương thích đã không được dùng nữa kể từ PHP 5. 6. 0. Kể từ PHP 7. 0. 0 gọi một phương thức không tĩnh tĩnh không được dùng nữa (ngay cả khi được gọi từ ngữ cảnh tương thích). Trước PHP 5. 6. 0, những 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 $this trở thành “không được xác định” khi một phương thức không tĩnh được gọi trong ngữ cảnh của một phương thức tĩnh

Từ khóa dành riêng trong PHP là gì?

Hướng dẫn PHP & MySQL - Từ dành riêng

Từ khóa mới trong PHP là gì?

Từ khóa new được dùng để tạo một đối tượng từ một lớp .

Từ khóa và chức năng là gì?

Các từ khóa chức năng chỉ định các chức năng mà Quản lý phân bổ nâng cao thực hiện và các giá trị mà nó sẽ sử dụng cho các chức năng đó, khi nó tìm thấy kết quả phù hợp bằng cách sử dụng các tiêu chí lựa chọn được liên kết

Bạn có thể sử dụng từ khóa cho tên biến trong PHP không?

Quy tắc đặt tên biến PHP . Tránh sử dụng từ khóa dành riêng cho tên biến của bạn , chẳng hạn như , or , name , for hoặc while. Tên biến có thể chứa dấu gạch dưới, số và chữ cái.