Hướng dẫn can a static method call a non static method in php? - một phương thức tĩnh có thể gọi một phương thức không tĩnh trong php không?

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.

class Foo {

    public function fun1[]
    {
        return 'non-static';
    }

    public static function fun2[]
    {
        return [new self]->fun1[];
    }
}

echo Foo::fun2[];
0 warning.

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[];
7

class Foo {

    public function fun1[]
    {
        return 'non-static';
    }

    public static function fun2[]
    {
        return [new self]->fun1[];
    }
}

echo Foo::fun2[];
8

class 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'];
0

payal001 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'];
1

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'];
2

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'];
3

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'];
4

artekpuck 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'];
5

Quả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'];
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'];
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'];
8

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'];
9

Ẩn danh ¶

17 năm trước

Foo $objInst = new Foo[];
$objInst->fun1[];
0

Foo $objInst = new Foo[];
$objInst->fun1[];
1

Foo $objInst = new Foo[];
$objInst->fun1[];
2

Foo $objInst = new Foo[];
$objInst->fun1[];
3

Foo $objInst = new Foo[];
$objInst->fun1[];
4

Foo $objInst = new Foo[];
$objInst->fun1[];
5

Foo $objInst = new Foo[];
$objInst->fun1[];
6

Foo $objInst = new Foo[];
$objInst->fun1[];
7

Ẩn danh ¶

17 năm trước

Foo $objInst = new Foo[];
$objInst->fun1[];
8

Foo $objInst = new Foo[];
$objInst->fun1[];
9

Foo $objInst = new Foo[];
$objInst->fun1[];
7

8 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
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

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'];
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
6

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
foo
7

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 $objInst = new Foo[];
$objInst->fun1[];
7

Ẩn danh ¶

17 năm trước

non-static1

non-static2

non-static3

Foo $objInst = new Foo[];
$objInst->fun1[];
7

8 năm trước

Aschmidt tại Anamera Dot Net

non-static5

non-static6

non-static7

non-static8

Rahul dot anand77 tại gmail dot com ¶

11 năm trước

non-static9

foo - bar0

foo - bar1

foo - bar2

artekpuck tại gmail dot com

4 năm trước

foo - bar3

foo - bar4

foo - bar5

foo - bar6

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

17 năm trước

foo - bar7

foo - bar8

foo - bar9

static0

static1

Foo $objInst = new Foo[];
$objInst->fun1[];
7

8 năm trước

14 năm trước

static3

static4

static5

static6

static7

static8

static9

static0

static1

static2

static3

static4

Foo $objInst = new Foo[];
$objInst->fun1[];
7

payal001 tại gmail dot com

2 năm trước

static6

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'];
8

static8

Ẩn danh ¶

Aschmidt tại Anamera Dot Net

static9

static0

static1

static2

Rahul dot anand77 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 {

    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'];
8

static9

8 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[];
00

class Foo {

    public function fun1[]
    {
        return 'non-static';
    }

    public static function fun2[]
    {
        return [new self]->fun1[];
    }
}

echo Foo::fun2[];
01

static4

class Foo {

    public function fun1[]
    {
        return 'non-static';
    }

    public static function fun2[]
    {
        return [new self]->fun1[];
    }
}

echo Foo::fun2[];
03

Quả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[];
04

class Foo {

    public function fun1[]
    {
        return 'non-static';
    }

    public static function fun2[]
    {
        return [new self]->fun1[];
    }
}

echo Foo::fun2[];
05

class Foo {

    public function fun1[]
    {
        return 'non-static';
    }

    public static function fun2[]
    {
        return [new self]->fun1[];
    }
}

echo Foo::fun2[];
06

class Foo {

    public function fun1[]
    {
        return 'non-static';
    }

    public static function fun2[]
    {
        return [new self]->fun1[];
    }
}

echo Foo::fun2[];
07

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'];
8

class Foo {

    public function fun1[]
    {
        return 'non-static';
    }

    public static function fun2[]
    {
        return [new self]->fun1[];
    }
}

echo Foo::fun2[];
09

Quả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[];
10

class Foo {

    public function fun1[]
    {
        return 'non-static';
    }

    public static function fun2[]
    {
        return [new self]->fun1[];
    }
}

echo Foo::fun2[];
11

class Foo {

    public function fun1[]
    {
        return 'non-static';
    }

    public static function fun2[]
    {
        return [new self]->fun1[];
    }
}

echo Foo::fun2[];
12

class Foo {

    public function fun1[]
    {
        return 'non-static';
    }

    public static function fun2[]
    {
        return [new self]->fun1[];
    }
}

echo Foo::fun2[];
13

Quả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[];
14

class Foo {

    public function fun1[]
    {
        return 'non-static';
    }

    public static function fun2[]
    {
        return [new self]->fun1[];
    }
}

echo Foo::fun2[];
15

class Foo {

    public function fun1[]
    {
        return 'non-static';
    }

    public static function fun2[]
    {
        return [new self]->fun1[];
    }
}

echo Foo::fun2[];
16

class Foo {

    public function fun1[]
    {
        return 'non-static';
    }

    public static function fun2[]
    {
        return [new self]->fun1[];
    }
}

echo Foo::fun2[];
17

8 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[];
18

class Foo {

    public function fun1[]
    {
        return 'non-static';
    }

    public static function fun2[]
    {
        return [new self]->fun1[];
    }
}

echo Foo::fun2[];
19

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'];
8

class Foo {

    public function fun1[]
    {
        return 'non-static';
    }

    public static function fun2[]
    {
        return [new self]->fun1[];
    }
}

echo Foo::fun2[];
21

8 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[];
22

payal001 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[];
23

class Foo {

    public function fun1[]
    {
        return 'non-static';
    }

    public static function fun2[]
    {
        return [new self]->fun1[];
    }
}

echo Foo::fun2[];
12

class Foo {

    public function fun1[]
    {
        return 'non-static';
    }

    public static function fun2[]
    {
        return [new self]->fun1[];
    }
}

echo Foo::fun2[];
25

payal001 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[];
26

class Foo {

    public function fun1[]
    {
        return 'non-static';
    }

    public static function fun2[]
    {
        return [new self]->fun1[];
    }
}

echo Foo::fun2[];
27

Foo $objInst = new Foo[];
$objInst->fun1[];
7

Hàm tĩnh có thể gọi hàm không tĩnh PHP không?

Trong Php 5, gọi các phương thức phi tĩnh sẽ tạo ra một cảnh báo E_strict. Trong Php 7, việc gọi các phương thức phi tĩnh một cách độc đáo được không dùng nữa và sẽ tạo ra cảnh báo e_deprecated. Xem Phương thức tĩnh [PHP.NET] để biết chi tiết. Trong ví dụ sau, phương thức foo [] được gọi là động trong khi thực sự nó là tĩnh.. In PHP 7, calling non-static methods statically is deprecated, and will generate an E_DEPRECATED warning. See Static methods [php.net] for details. In the following example, the method foo[] is called as dynamic while actually it is static.

Phương pháp tĩnh có thể gọi phương thức không tĩnh?

Một phương thức tĩnh chỉ có thể gọi các phương thức tĩnh khác;Nó không thể gọi một phương thức phi tĩnh.Một phương thức tĩnh có thể được gọi trực tiếp từ lớp, mà không phải tạo một thể hiện của lớp.Một phương pháp tĩnh chỉ có thể truy cập các biến tĩnh;Nó không thể truy cập các biến thể hiện.it cannot call a non-static method. A static method can be called directly from the class, without having to create an instance of the class. A static method can only access static variables; it cannot access instance variables.

Có thể truy cập chức năng tĩnh có thể truy cập các biến không tĩnh trong PHP không?

Phương thức tĩnh sẽ không có quyền truy cập vào $ này [vì không có $ điều này để nói trong bối cảnh tĩnh].Nếu bạn cần một tham chiếu đến đối tượng hiện tại trong phương thức tĩnh, nó không phải là phương thức tĩnh. A static method will not have access to $this [as there is no $this to talk about in a static context]. If you need a reference to the current object within the static method, it is not a static method.

Tại sao nó là bất hợp pháp đối với phương pháp tĩnh để gọi một phương pháp không tĩnh?

Phương pháp phi tĩnh là một phương pháp thực thi trong bối cảnh của một thể hiện.Không có một ví dụ, không có ý nghĩa gì khi gọi một, vì vậy trình biên dịch ngăn bạn làm như vậy - tức là nó là bất hợp pháp.Without an instance it makes no sense to call one, so the compiler prevents you from doing so - ie it's illegal.

Bài Viết Liên Quan

Chủ Đề