Hướng dẫn php use variable as static class name - php sử dụng biến làm tên lớp tĩnh

Tôi đang cố gắng truy cập một phương thức tĩnh, nhưng sử dụng một biến làm tên lớp. Điều này có thể? Tôi dường như đang có vấn đề với nó. Tôi muốn có thể làm điều gì đó như thế này:

class foo {
    public static function bar[] {
        echo 'test';
    }
}

$variable_class_name = 'foo';
$variable_class_name::bar[];

Và tôi cũng muốn có thể thực hiện tương tự bằng cách sử dụng các biến tĩnh.

hỏi ngày 20 tháng 2 năm 2011 lúc 20:59Feb 20, 2011 at 20:59

dqhendricksdqhendricksdqhendricks

18.9k10 Huy hiệu vàng48 Huy hiệu bạc82 Huy hiệu đồng10 gold badges48 silver badges82 bronze badges

4

Cú pháp đó chỉ được hỗ trợ trong PHP 5.3 trở lên. Các phiên bản trước không hiểu cú pháp đó, do đó lỗi phân tích cú pháp của bạn [T_PAAMAYIM_NEKUDOTAYIM đề cập đến toán tử ::].

Trong các phiên bản trước, bạn có thể thử call_user_func[], chuyển nó một mảng chứa tên lớp và tên phương thức của nó:

$variable_class_name = 'foo';
call_user_func[array[$variable_class_name, 'bar']];

Đã trả lời ngày 20 tháng 2 năm 2011 lúc 21:08Feb 20, 2011 at 21:08

BoltclockboltclockBoltClock

679K156 Huy hiệu vàng1365 Huy hiệu bạc1338 Huy hiệu đồng156 gold badges1365 silver badges1338 bronze badges

3

Bạn có thể sử dụng phản xạ cho PHP 5.1 trở lên:

class foo {
    public static $bar = 'foobar';
}

$class = 'foo';
$reflector = new ReflectionClass[$class];
echo $reflector->getStaticPropertyValue['bar'];

> foobar

Đã trả lời ngày 20 tháng 2 năm 2011 lúc 22:14Feb 20, 2011 at 22:14

David Harknessdavid HarknessDavid Harkness

35K10 Huy hiệu vàng111 Huy hiệu bạc129 Huy hiệu đồng10 gold badges111 silver badges129 bronze badges

0

Mẹo

Trang này mô tả việc sử dụng từ khóa static để xác định các phương thức và thuộc tính tĩnh. static cũng có thể được sử dụng để xác định các biến tĩnh và cho các ràng buộc tĩnh muộn. Vui lòng tham khảo các trang đó để biết thông tin về những ý nghĩa của static.

Khai báo các thuộc tính hoặc phương thức lớp là tĩnh giúp chúng có thể truy cập mà không cần khởi tạo lớp. Chúng cũng có thể được truy cập thống kê trong một đối tượng lớp khởi tạo.

Phương pháp tĩnh

Bởi vì các phương thức tĩnh có thể gọi được mà không có một thể hiện của đối tượng được tạo ra, nên việc biến giả $ này không có sẵn bên trong các phương thức được khai báo là tĩnh.

Cảnh báo

Gọi các phương thức phi tĩnh là ném một lỗi.Error.

Trước PHP 8.0.0, việc gọi các phương thức phi tĩnh được tính không thể dùng được và tạo ra cảnh báo ____10.

$variable_class_name = 'foo';
call_user_func[array[$variable_class_name, 'bar']];
0 warning.

Ví dụ #1 Phương pháp tĩnh ví dụ

$variable_class_name = 'foo';
call_user_func[array[$variable_class_name, 'bar']];
1

Tính chất tĩnh

Các thuộc tính tĩnh được truy cập bằng toán tử phân giải phạm vi [::] và không thể được truy cập thông qua toán tử đối tượng [

$variable_class_name = 'foo';
call_user_func[array[$variable_class_name, 'bar']];
3].

Có thể tham chiếu lớp bằng một biến. Giá trị của biến không thể là một từ khóa [ví dụ:

$variable_class_name = 'foo';
call_user_func[array[$variable_class_name, 'bar']];
4,
$variable_class_name = 'foo';
call_user_func[array[$variable_class_name, 'bar']];
5 và static].

Ví dụ #2 ví dụ thuộc tính tĩnh

$variable_class_name = 'foo';
call_user_func[array[$variable_class_name, 'bar']];
7

$variable_class_name = 'foo';
call_user_func[array[$variable_class_name, 'bar']];
8

$variable_class_name = 'foo';
call_user_func[array[$variable_class_name, 'bar']];
9

Đầu ra của ví dụ trên trong Php 8 tương tự như:

foo
foo

Notice: Accessing static property Foo::$my_static as non static in /in/V0Rvv on line 23

Warning: Undefined property: Foo::$my_static in /in/V0Rvv on line 23

foo
foo
foo
foo

Inkredredibl ¶

14 năm trước

class foo {
    public static $bar = 'foobar';
}

$class = 'foo';
$reflector = new ReflectionClass[$class];
echo $reflector->getStaticPropertyValue['bar'];

> foobar
0

payal001 tại gmail dot com

11 năm trước

class foo {
    public static $bar = 'foobar';
}

$class = 'foo';
$reflector = new ReflectionClass[$class];
echo $reflector->getStaticPropertyValue['bar'];

> foobar
1

class foo {
    public static $bar = 'foobar';
}

$class = 'foo';
$reflector = new ReflectionClass[$class];
echo $reflector->getStaticPropertyValue['bar'];

> foobar
2

class foo {
    public static $bar = 'foobar';
}

$class = 'foo';
$reflector = new ReflectionClass[$class];
echo $reflector->getStaticPropertyValue['bar'];

> foobar
3

class foo {
    public static $bar = 'foobar';
}

$class = 'foo';
$reflector = new ReflectionClass[$class];
echo $reflector->getStaticPropertyValue['bar'];

> foobar
4

artekpuck tại gmail dot com

4 năm trước

class foo {
    public static $bar = 'foobar';
}

$class = 'foo';
$reflector = new ReflectionClass[$class];
echo $reflector->getStaticPropertyValue['bar'];

> foobar
5

Quản trị viên tại Shopinson Dot Com ¶

2 năm trước

class foo {
    public static $bar = 'foobar';
}

$class = 'foo';
$reflector = new ReflectionClass[$class];
echo $reflector->getStaticPropertyValue['bar'];

> foobar
6

class foo {
    public static $bar = 'foobar';
}

$class = 'foo';
$reflector = new ReflectionClass[$class];
echo $reflector->getStaticPropertyValue['bar'];

> foobar
7

class foo {
    public static $bar = 'foobar';
}

$class = 'foo';
$reflector = new ReflectionClass[$class];
echo $reflector->getStaticPropertyValue['bar'];

> foobar
8

class foo {
    public static $bar = 'foobar';
}

$class = 'foo';
$reflector = new ReflectionClass[$class];
echo $reflector->getStaticPropertyValue['bar'];

> foobar
9

Ẩn danh ¶

17 năm trước

foo
foo

Notice: Accessing static property Foo::$my_static as non static in /in/V0Rvv on line 23

Warning: Undefined property: Foo::$my_static in /in/V0Rvv on line 23

foo
foo
foo
foo
0

foo
foo

Notice: Accessing static property Foo::$my_static as non static in /in/V0Rvv on line 23

Warning: Undefined property: Foo::$my_static in /in/V0Rvv on line 23

foo
foo
foo
foo
1

foo
foo

Notice: Accessing static property Foo::$my_static as non static in /in/V0Rvv on line 23

Warning: Undefined property: Foo::$my_static in /in/V0Rvv on line 23

foo
foo
foo
foo
2

foo
foo

Notice: Accessing static property Foo::$my_static as non static in /in/V0Rvv on line 23

Warning: Undefined property: Foo::$my_static in /in/V0Rvv on line 23

foo
foo
foo
foo
3

foo
foo

Notice: Accessing static property Foo::$my_static as non static in /in/V0Rvv on line 23

Warning: Undefined property: Foo::$my_static in /in/V0Rvv on line 23

foo
foo
foo
foo
4

foo
foo

Notice: Accessing static property Foo::$my_static as non static in /in/V0Rvv on line 23

Warning: Undefined property: Foo::$my_static in /in/V0Rvv on line 23

foo
foo
foo
foo
5

foo
foo

Notice: Accessing static property Foo::$my_static as non static in /in/V0Rvv on line 23

Warning: Undefined property: Foo::$my_static in /in/V0Rvv on line 23

foo
foo
foo
foo
6

foo
foo

Notice: Accessing static property Foo::$my_static as non static in /in/V0Rvv on line 23

Warning: Undefined property: Foo::$my_static in /in/V0Rvv on line 23

foo
foo
foo
foo
7

Ẩn danh ¶

17 năm trước

foo
foo

Notice: Accessing static property Foo::$my_static as non static in /in/V0Rvv on line 23

Warning: Undefined property: Foo::$my_static in /in/V0Rvv on line 23

foo
foo
foo
foo
8

foo
foo

Notice: Accessing static property Foo::$my_static as non static in /in/V0Rvv on line 23

Warning: Undefined property: Foo::$my_static in /in/V0Rvv on line 23

foo
foo
foo
foo
9

foo
foo

Notice: Accessing static property Foo::$my_static as non static in /in/V0Rvv on line 23

Warning: Undefined property: Foo::$my_static in /in/V0Rvv on line 23

foo
foo
foo
foo
7

9 năm trước

4 năm trước

T_PAAMAYIM_NEKUDOTAYIM1

T_PAAMAYIM_NEKUDOTAYIM2

T_PAAMAYIM_NEKUDOTAYIM3

T_PAAMAYIM_NEKUDOTAYIM4

class foo {
    public static $bar = 'foobar';
}

$class = 'foo';
$reflector = new ReflectionClass[$class];
echo $reflector->getStaticPropertyValue['bar'];

> foobar
8

T_PAAMAYIM_NEKUDOTAYIM6

Quản trị viên tại Shopinson Dot Com ¶

2 năm trước

T_PAAMAYIM_NEKUDOTAYIM7

T_PAAMAYIM_NEKUDOTAYIM8

T_PAAMAYIM_NEKUDOTAYIM9

foo
foo

Notice: Accessing static property Foo::$my_static as non static in /in/V0Rvv on line 23

Warning: Undefined property: Foo::$my_static in /in/V0Rvv on line 23

foo
foo
foo
foo
7

Ẩn danh ¶

17 năm trước

::1

::2

::3

foo
foo

Notice: Accessing static property Foo::$my_static as non static in /in/V0Rvv on line 23

Warning: Undefined property: Foo::$my_static in /in/V0Rvv on line 23

foo
foo
foo
foo
7

9 năm trước

Aschmidt tại Anamera Dot Net

::5

::6

::7

::8

Rahul dot anand77 tại gmail dot com ¶

11 năm trước

::9

call_user_func[]0

call_user_func[]1

call_user_func[]2

artekpuck tại gmail dot com

4 năm trước

call_user_func[]3

call_user_func[]4

call_user_func[]5

call_user_func[]6

Quản trị viên tại Shopinson Dot Com ¶

2 năm trước

call_user_func[]7

call_user_func[]8

call_user_func[]9

static0

static1

foo
foo

Notice: Accessing static property Foo::$my_static as non static in /in/V0Rvv on line 23

Warning: Undefined property: Foo::$my_static in /in/V0Rvv on line 23

foo
foo
foo
foo
7

Ẩn danh ¶

14 năm trước

static3

static4

static5

static6

static7

static8

static9

static0

static1

static2

static3

static4

foo
foo

Notice: Accessing static property Foo::$my_static as non static in /in/V0Rvv on line 23

Warning: Undefined property: Foo::$my_static in /in/V0Rvv on line 23

foo
foo
foo
foo
7

payal001 tại gmail dot com

2 năm trước

static6

class foo {
    public static $bar = 'foobar';
}

$class = 'foo';
$reflector = new ReflectionClass[$class];
echo $reflector->getStaticPropertyValue['bar'];

> foobar
8

static8

Ẩn danh ¶

14 năm trước

static9

static0

static1

static2

payal001 tại gmail dot com

11 năm trước

static3

static4

static5

artekpuck tại gmail dot com

17 năm trước

static6

static7

class foo {
    public static $bar = 'foobar';
}

$class = 'foo';
$reflector = new ReflectionClass[$class];
echo $reflector->getStaticPropertyValue['bar'];

> foobar
8

static9

9 năm trước

4 năm trước

$variable_class_name = 'foo';
call_user_func[array[$variable_class_name, 'bar']];
00

$variable_class_name = 'foo';
call_user_func[array[$variable_class_name, 'bar']];
01

static4

$variable_class_name = 'foo';
call_user_func[array[$variable_class_name, 'bar']];
03

Quản trị viên tại Shopinson Dot Com ¶

4 năm trước

$variable_class_name = 'foo';
call_user_func[array[$variable_class_name, 'bar']];
04

$variable_class_name = 'foo';
call_user_func[array[$variable_class_name, 'bar']];
05

$variable_class_name = 'foo';
call_user_func[array[$variable_class_name, 'bar']];
06

$variable_class_name = 'foo';
call_user_func[array[$variable_class_name, 'bar']];
07

class foo {
    public static $bar = 'foobar';
}

$class = 'foo';
$reflector = new ReflectionClass[$class];
echo $reflector->getStaticPropertyValue['bar'];

> foobar
8

$variable_class_name = 'foo';
call_user_func[array[$variable_class_name, 'bar']];
09

Quản trị viên tại Shopinson Dot Com ¶

4 năm trước

$variable_class_name = 'foo';
call_user_func[array[$variable_class_name, 'bar']];
10

$variable_class_name = 'foo';
call_user_func[array[$variable_class_name, 'bar']];
11

$variable_class_name = 'foo';
call_user_func[array[$variable_class_name, 'bar']];
12

$variable_class_name = 'foo';
call_user_func[array[$variable_class_name, 'bar']];
13

Quản trị viên tại Shopinson Dot Com ¶

2 năm trước

$variable_class_name = 'foo';
call_user_func[array[$variable_class_name, 'bar']];
14

$variable_class_name = 'foo';
call_user_func[array[$variable_class_name, 'bar']];
15

$variable_class_name = 'foo';
call_user_func[array[$variable_class_name, 'bar']];
16

$variable_class_name = 'foo';
call_user_func[array[$variable_class_name, 'bar']];
17

Ẩn danh ¶

17 năm trước

$variable_class_name = 'foo';
call_user_func[array[$variable_class_name, 'bar']];
18

$variable_class_name = 'foo';
call_user_func[array[$variable_class_name, 'bar']];
19

class foo {
    public static $bar = 'foobar';
}

$class = 'foo';
$reflector = new ReflectionClass[$class];
echo $reflector->getStaticPropertyValue['bar'];

> foobar
8

$variable_class_name = 'foo';
call_user_func[array[$variable_class_name, 'bar']];
21

9 năm trước

14 năm trước

$variable_class_name = 'foo';
call_user_func[array[$variable_class_name, 'bar']];
22

payal001 tại gmail dot com

14 năm trước

$variable_class_name = 'foo';
call_user_func[array[$variable_class_name, 'bar']];
23

$variable_class_name = 'foo';
call_user_func[array[$variable_class_name, 'bar']];
12

$variable_class_name = 'foo';
call_user_func[array[$variable_class_name, 'bar']];
25

payal001 tại gmail dot com

2 năm trước

$variable_class_name = 'foo';
call_user_func[array[$variable_class_name, 'bar']];
26

$variable_class_name = 'foo';
call_user_func[array[$variable_class_name, 'bar']];
27

foo
foo

Notice: Accessing static property Foo::$my_static as non static in /in/V0Rvv on line 23

Warning: Undefined property: Foo::$my_static in /in/V0Rvv on line 23

foo
foo
foo
foo
7

Làm thế nào có thể sử dụng biến tĩnh trong lớp trong PHP?

Từ khóa tĩnh được sử dụng để khai báo các thuộc tính và phương thức của một lớp là tĩnh. Các thuộc tính và phương thức tĩnh có thể được sử dụng mà không tạo ra một thể hiện của lớp. Từ khóa tĩnh cũng được sử dụng để khai báo các biến trong một hàm giữ giá trị của chúng sau khi hàm kết thúc.. Static properties and methods can be used without creating an instance of the class. The static keyword is also used to declare variables in a function which keep their value after the function has ended.

Chúng ta có thể tuyên bố lớp là tĩnh trong PHP không?

Trong PHP, chúng ta có thể có cả hai lớp tĩnh cũng như không tĩnh [khởi tạo].Giới thiệu: Một lớp tĩnh trong PHP là một loại lớp chỉ được khởi tạo một lần trong một chương trình.Nó phải chứa một thành viên tĩnh [biến] hoặc hàm thành viên tĩnh [phương thức] hoặc cả hai.we can have both static as well as non-static [instantiated] classes. Introduction: A static class in PHP is a type of class which is instantiated only once in a program. It must contain a static member [variable] or a static member function [method] or both.

Php biến tĩnh là gì?

Loại phạm vi biến cuối cùng mà tôi thảo luận được gọi là tĩnh.Trái ngược với các biến được khai báo là tham số chức năng, bị phá hủy trên lối ra của hàm, một biến tĩnh sẽ không mất giá trị của nó khi hàm thoát và vẫn sẽ giữ giá trị đó sẽ được gọi lại. that I discuss is known as static. In contrast to the variables declared as function parameters, which are destroyed on the function's exit, a static variable will not lose its value when the function exits and will still hold that value should the function be called again.

Làm thế nào chúng ta có thể gọi phương thức tĩnh bằng tên lớp?

Sau đó, chúng tôi gọi phương thức tĩnh bằng cách sử dụng tên lớp, Double Colon [: :] và tên phương thức [không tạo một thể hiện của lớp trước].using the class name, double colon [::], and the method name [without creating an instance of the class first].

Bài Viết Liên Quan

Chủ Đề