Tôi đang làm việc trên mã PHP.
Đây là mã mẫu để giải thích vấn đề của tôi:
class Foo {
public function fun1[] {
echo 'non-static';
}
public static function fun2[] {
echo "static" ;
//self::fun1[];
//Foo::fun1[];
}
}
Làm thế nào tôi có thể gọi phương thức phi tĩnh từ phương thức tĩnh?
Lưu ý: Cả hai chức năng đều được sử dụng trên toàn bộ trang web, không được biết đến. Tôi không thể thực hiện bất kỳ thay đổi nào trong bản chất tĩnh/không tĩnh của chúng. Both functions are used throughout the site, which is not known. I can't make any changes in the static/non-static nature of them.
Đã hỏi ngày 13 tháng 1 năm 2017 lúc 9:46Jan 13, 2017 at 9:46
RahulrahulRahul
17,9K7 Huy hiệu vàng40 Huy hiệu bạc58 Huy hiệu đồng7 gold badges40 silver badges58 bronze badges
6
Bạn phải tạo một đối tượng mới bên trong phương thức tĩnh để truy cập các phương thức không tĩnh bên trong lớp đó:
class Foo {
public function fun1[]
{
return 'non-static';
}
public static function fun2[]
{
return [new self]->fun1[];
}
}
echo Foo::fun2[];
Kết quả sẽ là non-static
Chỉnh sửa sau: Như đã thấy sự quan tâm trong việc chuyển các biến cho hàm tạo, tôi sẽ đăng một phiên bản cập nhật của lớp:
class Foo {
private $foo;
private $bar;
public function __construct[$foo, $bar]
{
$this->foo = $foo;
$this->bar = $bar;
}
public function fun1[]
{
return $this->foo . ' - ' . $this->bar;
}
public static function fun2[$foo, $bar]
{
return [new self[$foo, $bar]]->fun1[];
}
}
echo Foo::fun2['foo', 'bar'];
Kết quả sẽ là foo - bar
Đã trả lời ngày 13 tháng 1 năm 2017 lúc 9:51Jan 13, 2017 at 9:51
Mihai Mateimihai MateiMihai Matei
23.9K5 Huy hiệu vàng33 Huy hiệu bạc 50 Huy hiệu Đồng5 gold badges33 silver badges50 bronze badges
3
Sự khác biệt chính là bạn có thể gọi các phương thức tĩnh cho một lớp mà không phải khởi tạo một đối tượng của lớp đó. Vì vậy, trong phương pháp tĩnh của bạn, hãy thử
Foo $objInst = new Foo[];
$objInst->fun1[];
Nhưng tôi không thấy làm thế nào điều này sẽ có ý nghĩa trong bất kỳ bối cảnh nào.
Đã trả lời ngày 13 tháng 1 năm 2017 lúc 9:56Jan 13, 2017 at 9:56
1
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.
0 warning. class Foo {
public function fun1[]
{
return 'non-static';
}
public static function fun2[]
{
return [new self]->fun1[];
}
}
echo Foo::fun2[];
Ví dụ #1 Phương pháp tĩnh ví dụ
class Foo {
public function fun1[]
{
return 'non-static';
}
public static function fun2[]
{
return [new self]->fun1[];
}
}
echo Foo::fun2[];
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 [
class Foo {
public function fun1[]
{
return 'non-static';
}
public static function fun2[]
{
return [new self]->fun1[];
}
}
echo Foo::fun2[];
2] và không thể được truy cập thông qua toán tử đối tượng [class Foo {
public function fun1[]
{
return 'non-static';
}
public static function fun2[]
{
return [new self]->fun1[];
}
}
echo Foo::fun2[];
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ụ:
class Foo {
public function fun1[]
{
return 'non-static';
}
public static function fun2[]
{
return [new self]->fun1[];
}
}
echo Foo::fun2[];
4, class Foo {
public function fun1[]
{
return 'non-static';
}
public static function fun2[]
{
return [new self]->fun1[];
}
}
echo Foo::fun2[];
5 và static
].Ví dụ #2 ví dụ thuộc tính tĩnh
class Foo {
public function fun1[]
{
return 'non-static';
}
public static function fun2[]
{
return [new self]->fun1[];
}
}
echo Foo::fun2[];
7class Foo {
public function fun1[]
{
return 'non-static';
}
public static function fun2[]
{
return [new self]->fun1[];
}
}
echo Foo::fun2[];
8class Foo {
public function fun1[]
{
return 'non-static';
}
public static function fun2[]
{
return [new self]->fun1[];
}
}
echo Foo::fun2[];
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 {
private $foo;
private $bar;
public function __construct[$foo, $bar]
{
$this->foo = $foo;
$this->bar = $bar;
}
public function fun1[]
{
return $this->foo . ' - ' . $this->bar;
}
public static function fun2[$foo, $bar]
{
return [new self[$foo, $bar]]->fun1[];
}
}
echo Foo::fun2['foo', 'bar'];
0payal001 tại gmail dot com ¶
11 năm trước
class Foo {
private $foo;
private $bar;
public function __construct[$foo, $bar]
{
$this->foo = $foo;
$this->bar = $bar;
}
public function fun1[]
{
return $this->foo . ' - ' . $this->bar;
}
public static function fun2[$foo, $bar]
{
return [new self[$foo, $bar]]->fun1[];
}
}
echo Foo::fun2['foo', 'bar'];
1class Foo {
private $foo;
private $bar;
public function __construct[$foo, $bar]
{
$this->foo = $foo;
$this->bar = $bar;
}
public function fun1[]
{
return $this->foo . ' - ' . $this->bar;
}
public static function fun2[$foo, $bar]
{
return [new self[$foo, $bar]]->fun1[];
}
}
echo Foo::fun2['foo', 'bar'];
2class Foo {
private $foo;
private $bar;
public function __construct[$foo, $bar]
{
$this->foo = $foo;
$this->bar = $bar;
}
public function fun1[]
{
return $this->foo . ' - ' . $this->bar;
}
public static function fun2[$foo, $bar]
{
return [new self[$foo, $bar]]->fun1[];
}
}
echo Foo::fun2['foo', 'bar'];
3class Foo {
private $foo;
private $bar;
public function __construct[$foo, $bar]
{
$this->foo = $foo;
$this->bar = $bar;
}
public function fun1[]
{
return $this->foo . ' - ' . $this->bar;
}
public static function fun2[$foo, $bar]
{
return [new self[$foo, $bar]]->fun1[];
}
}
echo Foo::fun2['foo', 'bar'];
4artekpuck tại gmail dot com ¶
4 năm trước
class Foo {
private $foo;
private $bar;
public function __construct[$foo, $bar]
{
$this->foo = $foo;
$this->bar = $bar;
}
public function fun1[]
{
return $this->foo . ' - ' . $this->bar;
}
public static function fun2[$foo, $bar]
{
return [new self[$foo, $bar]]->fun1[];
}
}
echo Foo::fun2['foo', 'bar'];
5Quản trị viên tại Shopinson Dot Com ¶ ¶
2 năm trước
class Foo {
private $foo;
private $bar;
public function __construct[$foo, $bar]
{
$this->foo = $foo;
$this->bar = $bar;
}
public function fun1[]
{
return $this->foo . ' - ' . $this->bar;
}
public static function fun2[$foo, $bar]
{
return [new self[$foo, $bar]]->fun1[];
}
}
echo Foo::fun2['foo', 'bar'];
6class Foo {
private $foo;
private $bar;
public function __construct[$foo, $bar]
{
$this->foo = $foo;
$this->bar = $bar;
}
public function fun1[]
{
return $this->foo . ' - ' . $this->bar;
}
public static function fun2[$foo, $bar]
{
return [new self[$foo, $bar]]->fun1[];
}
}
echo Foo::fun2['foo', 'bar'];
7class Foo {
private $foo;
private $bar;
public function __construct[$foo, $bar]
{
$this->foo = $foo;
$this->bar = $bar;
}
public function fun1[]
{
return $this->foo . ' - ' . $this->bar;
}
public static function fun2[$foo, $bar]
{
return [new self[$foo, $bar]]->fun1[];
}
}
echo Foo::fun2['foo', 'bar'];
8class Foo {
private $foo;
private $bar;
public function __construct[$foo, $bar]
{
$this->foo = $foo;
$this->bar = $bar;
}
public function fun1[]
{
return $this->foo . ' - ' . $this->bar;
}
public static function fun2[$foo, $bar]
{
return [new self[$foo, $bar]]->fun1[];
}
}
echo Foo::fun2['foo', 'bar'];
9Ẩn danh ¶ ¶
17 năm trước
Foo $objInst = new Foo[];
$objInst->fun1[];
0Foo $objInst = new Foo[];
$objInst->fun1[];
1Foo $objInst = new Foo[];
$objInst->fun1[];
2Foo $objInst = new Foo[];
$objInst->fun1[];
3Foo $objInst = new Foo[];
$objInst->fun1[];
4Foo $objInst = new Foo[];
$objInst->fun1[];
5Foo $objInst = new Foo[];
$objInst->fun1[];
6Foo $objInst = new Foo[];
$objInst->fun1[];
7Ẩn danh ¶ ¶
17 năm trước
Foo $objInst = new Foo[];
$objInst->fun1[];
8Foo $objInst = new Foo[];
$objInst->fun1[];
9Foo $objInst = new Foo[];
$objInst->fun1[];
78 năm trước ¶
4 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 foo1
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 foo2
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 foo3
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 foo4
class Foo {
private $foo;
private $bar;
public function __construct[$foo, $bar]
{
$this->foo = $foo;
$this->bar = $bar;
}
public function fun1[]
{
return $this->foo . ' - ' . $this->bar;
}
public static function fun2[$foo, $bar]
{
return [new self[$foo, $bar]]->fun1[];
}
}
echo Foo::fun2['foo', 'bar'];
8foo 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 foo6
Quản trị viên tại Shopinson Dot Com ¶ ¶
2 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 foo7
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 foo8
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 foo9
Foo $objInst = new Foo[];
$objInst->fun1[];
7Ẩn danh ¶ ¶
17 năm trước
non-static
1
non-static
2
non-static
3
Foo $objInst = new Foo[];
$objInst->fun1[];
78 năm trước ¶
Aschmidt tại Anamera Dot Net
non-static
5
non-static
6
non-static
7
non-static
8Rahul dot anand77 tại gmail dot com ¶ ¶
11 năm trước
non-static
9
foo - bar
0
foo - bar
1
foo - bar
2artekpuck tại gmail dot com ¶
4 năm trước
foo - bar
3
foo - bar
4
foo - bar
5
foo - bar
6Quản trị viên tại Shopinson Dot Com ¶ ¶
17 năm trước
foo - bar
7
foo - bar
8
foo - bar
9
static
0
static
1
Foo $objInst = new Foo[];
$objInst->fun1[];
78 năm trước ¶
14 năm trước
static
3
static
4
static
5
static
6
static
7
static
8
static
9
static
0
static
1
static
2
static
3
static
4
Foo $objInst = new Foo[];
$objInst->fun1[];
7payal001 tại gmail dot com ¶
2 năm trước
static
6
class Foo {
private $foo;
private $bar;
public function __construct[$foo, $bar]
{
$this->foo = $foo;
$this->bar = $bar;
}
public function fun1[]
{
return $this->foo . ' - ' . $this->bar;
}
public static function fun2[$foo, $bar]
{
return [new self[$foo, $bar]]->fun1[];
}
}
echo Foo::fun2['foo', 'bar'];
8static
8Ẩn danh ¶ ¶
Aschmidt tại Anamera Dot Net
static
9
static
0
static
1
static
2Rahul dot anand77 tại gmail dot com ¶ ¶
11 năm trước
static
3
static
4
static
5artekpuck tại gmail dot com ¶
17 năm trước
static
6
static
7
class Foo {
private $foo;
private $bar;
public function __construct[$foo, $bar]
{
$this->foo = $foo;
$this->bar = $bar;
}
public function fun1[]
{
return $this->foo . ' - ' . $this->bar;
}
public static function fun2[$foo, $bar]
{
return [new self[$foo, $bar]]->fun1[];
}
}
echo Foo::fun2['foo', 'bar'];
8static
98 năm trước ¶
4 năm trước
class Foo {
public function fun1[]
{
return 'non-static';
}
public static function fun2[]
{
return [new self]->fun1[];
}
}
echo Foo::fun2[];
00class Foo {
public function fun1[]
{
return 'non-static';
}
public static function fun2[]
{
return [new self]->fun1[];
}
}
echo Foo::fun2[];
01static
4
class Foo {
public function fun1[]
{
return 'non-static';
}
public static function fun2[]
{
return [new self]->fun1[];
}
}
echo Foo::fun2[];
03Quản trị viên tại Shopinson Dot Com ¶ ¶
4 năm trước
class Foo {
public function fun1[]
{
return 'non-static';
}
public static function fun2[]
{
return [new self]->fun1[];
}
}
echo Foo::fun2[];
04class Foo {
public function fun1[]
{
return 'non-static';
}
public static function fun2[]
{
return [new self]->fun1[];
}
}
echo Foo::fun2[];
05class Foo {
public function fun1[]
{
return 'non-static';
}
public static function fun2[]
{
return [new self]->fun1[];
}
}
echo Foo::fun2[];
06class Foo {
public function fun1[]
{
return 'non-static';
}
public static function fun2[]
{
return [new self]->fun1[];
}
}
echo Foo::fun2[];
07class Foo {
private $foo;
private $bar;
public function __construct[$foo, $bar]
{
$this->foo = $foo;
$this->bar = $bar;
}
public function fun1[]
{
return $this->foo . ' - ' . $this->bar;
}
public static function fun2[$foo, $bar]
{
return [new self[$foo, $bar]]->fun1[];
}
}
echo Foo::fun2['foo', 'bar'];
8class Foo {
public function fun1[]
{
return 'non-static';
}
public static function fun2[]
{
return [new self]->fun1[];
}
}
echo Foo::fun2[];
09Quản trị viên tại Shopinson Dot Com ¶ ¶
4 năm trước
class Foo {
public function fun1[]
{
return 'non-static';
}
public static function fun2[]
{
return [new self]->fun1[];
}
}
echo Foo::fun2[];
10class Foo {
public function fun1[]
{
return 'non-static';
}
public static function fun2[]
{
return [new self]->fun1[];
}
}
echo Foo::fun2[];
11class Foo {
public function fun1[]
{
return 'non-static';
}
public static function fun2[]
{
return [new self]->fun1[];
}
}
echo Foo::fun2[];
12class Foo {
public function fun1[]
{
return 'non-static';
}
public static function fun2[]
{
return [new self]->fun1[];
}
}
echo Foo::fun2[];
13Quản trị viên tại Shopinson Dot Com ¶ ¶
17 năm trước
class Foo {
public function fun1[]
{
return 'non-static';
}
public static function fun2[]
{
return [new self]->fun1[];
}
}
echo Foo::fun2[];
14class Foo {
public function fun1[]
{
return 'non-static';
}
public static function fun2[]
{
return [new self]->fun1[];
}
}
echo Foo::fun2[];
15class Foo {
public function fun1[]
{
return 'non-static';
}
public static function fun2[]
{
return [new self]->fun1[];
}
}
echo Foo::fun2[];
16class Foo {
public function fun1[]
{
return 'non-static';
}
public static function fun2[]
{
return [new self]->fun1[];
}
}
echo Foo::fun2[];
178 năm trước ¶
17 năm trước
class Foo {
public function fun1[]
{
return 'non-static';
}
public static function fun2[]
{
return [new self]->fun1[];
}
}
echo Foo::fun2[];
18class Foo {
public function fun1[]
{
return 'non-static';
}
public static function fun2[]
{
return [new self]->fun1[];
}
}
echo Foo::fun2[];
19class Foo {
private $foo;
private $bar;
public function __construct[$foo, $bar]
{
$this->foo = $foo;
$this->bar = $bar;
}
public function fun1[]
{
return $this->foo . ' - ' . $this->bar;
}
public static function fun2[$foo, $bar]
{
return [new self[$foo, $bar]]->fun1[];
}
}
echo Foo::fun2['foo', 'bar'];
8class Foo {
public function fun1[]
{
return 'non-static';
}
public static function fun2[]
{
return [new self]->fun1[];
}
}
echo Foo::fun2[];
218 năm trước ¶
14 năm trước
class Foo {
public function fun1[]
{
return 'non-static';
}
public static function fun2[]
{
return [new self]->fun1[];
}
}
echo Foo::fun2[];
22payal001 tại gmail dot com ¶
14 năm trước
class Foo {
public function fun1[]
{
return 'non-static';
}
public static function fun2[]
{
return [new self]->fun1[];
}
}
echo Foo::fun2[];
23class Foo {
public function fun1[]
{
return 'non-static';
}
public static function fun2[]
{
return [new self]->fun1[];
}
}
echo Foo::fun2[];
12class Foo {
public function fun1[]
{
return 'non-static';
}
public static function fun2[]
{
return [new self]->fun1[];
}
}
echo Foo::fun2[];
25payal001 tại gmail dot com ¶
2 năm trước
class Foo {
public function fun1[]
{
return 'non-static';
}
public static function fun2[]
{
return [new self]->fun1[];
}
}
echo Foo::fun2[];
26class Foo {
public function fun1[]
{
return 'non-static';
}
public static function fun2[]
{
return [new self]->fun1[];
}
}
echo Foo::fun2[];
27Foo $objInst = new Foo[];
$objInst->fun1[];
7