Hướng dẫn use class in class php - sử dụng lớp trong lớp php

Này, tôi đang tự hỏi làm thế nào điều này được thực hiện như khi tôi thử mã sau trong một hàm của lớp nó tạo ra một số lỗi PHP mà tôi không thể bắt được

public $tasks;
$this->tasks = new tasks($this);
$this->tasks->test();

Tôi không biết tại sao sự khởi đầu của lớp cũng yêu cầu $ đây là một tham số: s

cảm ơn

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();

Đã hỏi ngày 23 tháng 9 năm 2009 lúc 21:31Sep 23, 2009 at 21:31

Hướng dẫn use class in class php - sử dụng lớp trong lớp php

SupernovahsupernovahSupernovah

1.88612 Huy hiệu vàng34 Huy hiệu bạc 50 Huy hiệu Đồng12 gold badges34 silver badges50 bronze badges

2

Bạn không thể khai báo các tác vụ $ công khai bên trong phương thức của lớp (chức năng.) Nếu bạn không cần sử dụng đối tượng tác vụ bên ngoài phương thức đó, bạn chỉ có thể làm:

$tasks = new Tasks($this);
$tasks->test();

Bạn chỉ cần sử dụng "$ this->" khi bạn sử dụng một biến mà bạn muốn có sẵn trong suốt lớp.

Hai lựa chọn của bạn:

class Foo
{
    public $tasks;

    function doStuff()
    {
        $this->tasks = new Tasks();
        $this->tasks->test();
    }

    function doSomethingElse()
    {
        // you'd have to check that the method above ran and instantiated this
        // and that $this->tasks is a tasks object
        $this->tasks->blah();
    }

}

hoặc

class Foo
{
    function doStuff()
    {
        $tasks = new tasks();
        $tasks->test();
    }
}

với mã của bạn:

class Admin
{
    function validate()
    {
        // added this so it will execute
        $_SESSION['level'] = 7;

        if (! $_SESSION['level'] == 7) {
            // barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        } else {
            $tasks = new Tasks();
            $tasks->test();
            $this->showPanel();
        }
    }

    function showPanel()
    {
        // added this for test
    }
}
class Tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new Admin();
$admin->validate();

ra ngoài

73.4K20 Huy hiệu vàng146 Huy hiệu bạc216 Huy hiệu đồng20 gold badges146 silver badges216 bronze badges

Đã trả lời ngày 23 tháng 9 năm 2009 lúc 21:37Sep 23, 2009 at 21:37

Hướng dẫn use class in class php - sử dụng lớp trong lớp php

Lance Kidwelllance KidwellLance Kidwell

8531 Huy hiệu vàng14 Huy hiệu bạc18 Huy hiệu đồng1 gold badge14 silver badges18 bronze badges

0

Bạn có vấn đề là với dòng mã này:

public $tasks;
$this->tasks = new tasks();
$this->tasks->test();
$this->showPanel();

Từ khóa

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
6 được sử dụng trong định nghĩa của lớp, không phải trong một phương thức của lớp. Trong PHP, bạn thậm chí không cần phải khai báo biến thành viên trong lớp, bạn chỉ có thể làm
class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
7 và nó được thêm vào cho bạn.

Đã trả lời ngày 23 tháng 9 năm 2009 lúc 21:44Sep 23, 2009 at 21:44

Davidtbernaldavidtbernaldavidtbernal

13.2k9 Huy hiệu vàng44 Huy hiệu bạc59 Huy hiệu đồng9 gold badges44 silver badges59 bronze badges

class

Hayley Watson ¶

4 năm trước

WBCarts tại Juno Dot Com ¶

Ghi chú về STDCLASS ¶

$tasks = new Tasks($this);
$tasks->test();
0

13 năm trước

Cảnh báo

Gọi một phương thức phi tĩnh là ném một lỗi. Trước Php 8.0.0, điều này sẽ tạo ra thông báo không nhận được và $ Điều này sẽ không được xác định.Error. Prior to PHP 8.0.0, this would generate a deprecation notice, and $this would be undefined.

Ví dụ #2 Một số ví dụ về $ này là giả biến

$tasks = new Tasks($this);
$tasks->test();
1

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
8

$tasks = new Tasks($this);
$tasks->test();
3

Đầu ra của ví dụ trên trong Php 7:

$this is defined (A)

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 27
$this is not defined.

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.

Deprecated: Non-static method B::bar() should not be called statically in %s  on line 32

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.

Đầu ra của ví dụ trên trong Php 8:

$this is defined (A)

Fatal error: Uncaught Error: Non-static method A::foo() cannot be called statically in %s :27
Stack trace:
#0 {main}
  thrown in %s  on line 27

new

Để tạo một thể hiện của một lớp, từ khóa

$tasks = new Tasks($this);
$tasks->test();
4 phải được sử dụng. Một đối tượng sẽ luôn được tạo trừ khi đối tượng có một hàm tạo được xác định để ném một ngoại lệ về lỗi. Các lớp nên được xác định trước khi khởi tạo (và trong một số trường hợp, đây là một yêu cầu).

Nếu một chuỗi chứa tên của một lớp được sử dụng với

$tasks = new Tasks($this);
$tasks->test();
4, một thể hiện mới của lớp đó sẽ được tạo. Nếu lớp nằm trong không gian tên, tên đủ điều kiện của nó phải được sử dụng khi làm điều này.string containing the name of a class is used with
$tasks = new Tasks($this);
$tasks->test();
4, a new instance of that class will be created. If the class is in a namespace, its fully qualified name must be used when doing this.

Ghi chú::

Nếu không có đối số nào được chuyển cho hàm tạo của lớp, ngoặc đơn sau khi tên lớp có thể bị bỏ qua.

Ví dụ #3 Tạo một thể hiện

$tasks = new Tasks($this);
$tasks->test();
6

Kể từ Php 8.0.0, sử dụng

$tasks = new Tasks($this);
$tasks->test();
4 với các biểu thức tùy ý được hỗ trợ. Điều này cho phép khởi tạo phức tạp hơn nếu biểu thức tạo ra một chuỗi. Các biểu thức phải được bọc trong ngoặc đơn.string. The expressions must be wrapped in parentheses.

Ví dụ #4 Tạo một thể hiện bằng cách sử dụng biểu thức tùy ý

Trong ví dụ đã cho, chúng tôi hiển thị nhiều ví dụ về các biểu thức tùy ý hợp lệ tạo ra một tên lớp. Điều này cho thấy một cuộc gọi đến một hàm, nối chuỗi và hằng số

$tasks = new Tasks($this);
$tasks->test();
8.
$tasks = new Tasks($this);
$tasks->test();
8
constant.

$tasks = new Tasks($this);
$tasks->test();
9

class Foo
{
    public $tasks;

    function doStuff()
    {
        $this->tasks = new Tasks();
        $this->tasks->test();
    }

    function doSomethingElse()
    {
        // you'd have to check that the method above ran and instantiated this
        // and that $this->tasks is a tasks object
        $this->tasks->blah();
    }

}
0

class Foo
{
    public $tasks;

    function doStuff()
    {
        $this->tasks = new Tasks();
        $this->tasks->test();
    }

    function doSomethingElse()
    {
        // you'd have to check that the method above ran and instantiated this
        // and that $this->tasks is a tasks object
        $this->tasks->blah();
    }

}
1

Đầu ra của ví dụ trên trong Php 8:

object(ClassA)#1 (0) {
}
object(ClassB)#1 (0) {
}
object(ClassC)#1 (0) {
}
object(ClassD)#1 (0) {
}

Để tạo một thể hiện của một lớp, từ khóa

$tasks = new Tasks($this);
$tasks->test();
4 phải được sử dụng. Một đối tượng sẽ luôn được tạo trừ khi đối tượng có một hàm tạo được xác định để ném một ngoại lệ về lỗi. Các lớp nên được xác định trước khi khởi tạo (và trong một số trường hợp, đây là một yêu cầu).

Nếu một chuỗi chứa tên của một lớp được sử dụng với

$tasks = new Tasks($this);
$tasks->test();
4, một thể hiện mới của lớp đó sẽ được tạo. Nếu lớp nằm trong không gian tên, tên đủ điều kiện của nó phải được sử dụng khi làm điều này.

Ghi chú:

class Foo
{
    public $tasks;

    function doStuff()
    {
        $this->tasks = new Tasks();
        $this->tasks->test();
    }

    function doSomethingElse()
    {
        // you'd have to check that the method above ran and instantiated this
        // and that $this->tasks is a tasks object
        $this->tasks->blah();
    }

}
4

class Foo
{
    public $tasks;

    function doStuff()
    {
        $this->tasks = new Tasks();
        $this->tasks->test();
    }

    function doSomethingElse()
    {
        // you'd have to check that the method above ran and instantiated this
        // and that $this->tasks is a tasks object
        $this->tasks->blah();
    }

}
5

class Foo
{
    public $tasks;

    function doStuff()
    {
        $this->tasks = new Tasks();
        $this->tasks->test();
    }

    function doSomethingElse()
    {
        // you'd have to check that the method above ran and instantiated this
        // and that $this->tasks is a tasks object
        $this->tasks->blah();
    }

}
6

Nếu không có đối số nào được chuyển cho hàm tạo của lớp, ngoặc đơn sau khi tên lớp có thể bị bỏ qua.

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
0

Ví dụ #3 Tạo một thể hiện

Kể từ Php 8.0.0, sử dụng

$tasks = new Tasks($this);
$tasks->test();
4 với các biểu thức tùy ý được hỗ trợ. Điều này cho phép khởi tạo phức tạp hơn nếu biểu thức tạo ra một chuỗi. Các biểu thức phải được bọc trong ngoặc đơn.

class Foo
{
    public $tasks;

    function doStuff()
    {
        $this->tasks = new Tasks();
        $this->tasks->test();
    }

    function doSomethingElse()
    {
        // you'd have to check that the method above ran and instantiated this
        // and that $this->tasks is a tasks object
        $this->tasks->blah();
    }

}
7

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
8

class Foo
{
    public $tasks;

    function doStuff()
    {
        $this->tasks = new Tasks();
        $this->tasks->test();
    }

    function doSomethingElse()
    {
        // you'd have to check that the method above ran and instantiated this
        // and that $this->tasks is a tasks object
        $this->tasks->blah();
    }

}
9

Nếu không có đối số nào được chuyển cho hàm tạo của lớp, ngoặc đơn sau khi tên lớp có thể bị bỏ qua.

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
1

Ví dụ #3 Tạo một thể hiện

Kể từ Php 8.0.0, sử dụng

$tasks = new Tasks($this);
$tasks->test();
4 với các biểu thức tùy ý được hỗ trợ. Điều này cho phép khởi tạo phức tạp hơn nếu biểu thức tạo ra một chuỗi. Các biểu thức phải được bọc trong ngoặc đơn.

class Foo
{
    function doStuff()
    {
        $tasks = new tasks();
        $tasks->test();
    }
}
0

Ví dụ #4 Tạo một thể hiện bằng cách sử dụng biểu thức tùy ý

Trong ví dụ đã cho, chúng tôi hiển thị nhiều ví dụ về các biểu thức tùy ý hợp lệ tạo ra một tên lớp. Điều này cho thấy một cuộc gọi đến một hàm, nối chuỗi và hằng số

$tasks = new Tasks($this);
$tasks->test();
8.: Prior to PHP 7.1, the arguments are not evaluated if there is no constructor function defined.

Trong bối cảnh lớp, có thể tạo một đối tượng mới bằng class Foo { public $tasks; function doStuff() { $this->tasks = new Tasks(); $this->tasks->test(); } function doSomethingElse() { // you'd have to check that the method above ran and instantiated this // and that $this->tasks is a tasks object $this->tasks->blah(); } } 2 và class Foo { public $tasks; function doStuff() { $this->tasks = new Tasks(); $this->tasks->test(); } function doSomethingElse() { // you'd have to check that the method above ran and instantiated this // and that $this->tasks is a tasks object $this->tasks->blah(); } } 3.

Khi gán một thể hiện đã được tạo của một lớp cho một biến mới, biến mới sẽ truy cập cùng một thể hiện với đối tượng được gán. Hành vi này là như nhau khi chuyển các trường hợp cho một hàm. Một bản sao của một đối tượng đã được tạo có thể được tạo bằng cách nhân bản nó.

Ví dụ #5 gán đối tượng

class Foo
{
    function doStuff()
    {
        $tasks = new tasks();
        $tasks->test();
    }
}
1

class Foo
{
    function doStuff()
    {
        $tasks = new tasks();
        $tasks->test();
    }
}
2

class Foo
{
    function doStuff()
    {
        $tasks = new tasks();
        $tasks->test();
    }
}
3

Nếu không có đối số nào được chuyển cho hàm tạo của lớp, ngoặc đơn sau khi tên lớp có thể bị bỏ qua.

Ví dụ #3 Tạo một thể hiện

Kể từ Php 8.0.0, sử dụng

$tasks = new Tasks($this);
$tasks->test();
4 với các biểu thức tùy ý được hỗ trợ. Điều này cho phép khởi tạo phức tạp hơn nếu biểu thức tạo ra một chuỗi. Các biểu thức phải được bọc trong ngoặc đơn.

class Foo
{
    function doStuff()
    {
        $tasks = new tasks();
        $tasks->test();
    }
}
4

class Foo
{
    function doStuff()
    {
        $tasks = new tasks();
        $tasks->test();
    }
}
2

class Foo
{
    function doStuff()
    {
        $tasks = new tasks();
        $tasks->test();
    }
}
6

Nếu không có đối số nào được chuyển cho hàm tạo của lớp, ngoặc đơn sau khi tên lớp có thể bị bỏ qua.

extends

Ví dụ #3 Tạo một thể hiện

Kể từ Php 8.0.0, sử dụng

$tasks = new Tasks($this);
$tasks->test();
4 với các biểu thức tùy ý được hỗ trợ. Điều này cho phép khởi tạo phức tạp hơn nếu biểu thức tạo ra một chuỗi. Các biểu thức phải được bọc trong ngoặc đơn.

Ví dụ #4 Tạo một thể hiện bằng cách sử dụng biểu thức tùy ý: As of PHP 8.1.0, constants may be declared as final.

Trong ví dụ đã cho, chúng tôi hiển thị nhiều ví dụ về các biểu thức tùy ý hợp lệ tạo ra một tên lớp. Điều này cho thấy một cuộc gọi đến một hàm, nối chuỗi và hằng số

$tasks = new Tasks($this);
$tasks->test();
8.

class Foo
{
    function doStuff()
    {
        $tasks = new tasks();
        $tasks->test();
    }
}
8

Nếu không có đối số nào được chuyển cho hàm tạo của lớp, ngoặc đơn sau khi tên lớp có thể bị bỏ qua.

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
2

Ví dụ #3 Tạo một thể hiện

Kể từ Php 8.0.0, sử dụng

$tasks = new Tasks($this);
$tasks->test();
4 với các biểu thức tùy ý được hỗ trợ. Điều này cho phép khởi tạo phức tạp hơn nếu biểu thức tạo ra một chuỗi. Các biểu thức phải được bọc trong ngoặc đơn.
class Foo
{
    function doStuff()
    {
        $tasks = new tasks();
        $tasks->test();
    }
}
9
level error is generated. A signature is compatible if it respects the variance rules, makes a mandatory parameter optional, and if any new parameters are optional. This is known as the Liskov Substitution Principle, or LSP for short. The constructor, and
class Admin
{
    function validate()
    {
        // added this so it will execute
        $_SESSION['level'] = 7;

        if (! $_SESSION['level'] == 7) {
            // barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        } else {
            $tasks = new Tasks();
            $tasks->test();
            $this->showPanel();
        }
    }

    function showPanel()
    {
        // added this for test
    }
}
class Tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new Admin();
$admin->validate();
0 methods are exempt from these signature compatibility rules, and thus won't emit a fatal error in case of a signature mismatch.

Ví dụ #11 Phương pháp trẻ em tương thích

class Admin
{
    function validate()
    {
        // added this so it will execute
        $_SESSION['level'] = 7;

        if (! $_SESSION['level'] == 7) {
            // barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        } else {
            $tasks = new Tasks();
            $tasks->test();
            $this->showPanel();
        }
    }

    function showPanel()
    {
        // added this for test
    }
}
class Tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new Admin();
$admin->validate();
1

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
8

class Admin
{
    function validate()
    {
        // added this so it will execute
        $_SESSION['level'] = 7;

        if (! $_SESSION['level'] == 7) {
            // barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        } else {
            $tasks = new Tasks();
            $tasks->test();
            $this->showPanel();
        }
    }

    function showPanel()
    {
        // added this for test
    }
}
class Tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new Admin();
$admin->validate();
3

Ví dụ trên sẽ xuất ra:

Các ví dụ sau đây chứng minh rằng một phương thức con sẽ loại bỏ tham số hoặc làm cho một tham số tùy chọn bắt buộc, không tương thích với phương thức mẹ.

Ví dụ #12 Lỗi gây tử vong khi phương pháp con xóa tham số

class Admin
{
    function validate()
    {
        // added this so it will execute
        $_SESSION['level'] = 7;

        if (! $_SESSION['level'] == 7) {
            // barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        } else {
            $tasks = new Tasks();
            $tasks->test();
            $this->showPanel();
        }
    }

    function showPanel()
    {
        // added this for test
    }
}
class Tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new Admin();
$admin->validate();
4

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
8

class Admin
{
    function validate()
    {
        // added this so it will execute
        $_SESSION['level'] = 7;

        if (! $_SESSION['level'] == 7) {
            // barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        } else {
            $tasks = new Tasks();
            $tasks->test();
            $this->showPanel();
        }
    }

    function showPanel()
    {
        // added this for test
    }
}
class Tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new Admin();
$admin->validate();
6

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

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
3

Ví dụ #13 Lỗi nghiêm trọng khi phương thức con bắt buộc tham số tùy chọn bắt buộc

class Admin
{
    function validate()
    {
        // added this so it will execute
        $_SESSION['level'] = 7;

        if (! $_SESSION['level'] == 7) {
            // barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        } else {
            $tasks = new Tasks();
            $tasks->test();
            $this->showPanel();
        }
    }

    function showPanel()
    {
        // added this for test
    }
}
class Tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new Admin();
$admin->validate();
4

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
8

class Admin
{
    function validate()
    {
        // added this so it will execute
        $_SESSION['level'] = 7;

        if (! $_SESSION['level'] == 7) {
            // barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        } else {
            $tasks = new Tasks();
            $tasks->test();
            $this->showPanel();
        }
    }

    function showPanel()
    {
        // added this for test
    }
}
class Tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new Admin();
$admin->validate();
9

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

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
4

Ví dụ #13 Lỗi nghiêm trọng khi phương thức con bắt buộc tham số tùy chọn bắt buộc

Cảnh báoError if named arguments are used.

Đổi tên tham số của một phương thức trong một lớp con không phải là sự không tương thích của chữ ký. Tuy nhiên, điều này không được khuyến khích vì nó sẽ dẫn đến một lỗi thời gian chạy nếu các đối số được đặt tên được sử dụng.

public $tasks;
$this->tasks = new tasks();
$this->tasks->test();
$this->showPanel();
0

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
8

public $tasks;
$this->tasks = new tasks();
$this->tasks->test();
$this->showPanel();
2

Ví dụ #Lỗi 14 Khi sử dụng các đối số và tham số được đặt tên được đổi tên trong lớp con

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
5

::class

Ví dụ trên sẽ xuất ra một cái gì đó tương tự như:

Từ khóa

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
8 cũng được sử dụng để giải quyết tên lớp. Để có được tên đủ điều kiện của lớp
public $tasks;
$this->tasks = new tasks();
$this->tasks->test();
$this->showPanel();
4 sử dụng
public $tasks;
$this->tasks = new tasks();
$this->tasks->test();
$this->showPanel();
5. Điều này đặc biệt hữu ích với các lớp theo tên.

public $tasks;
$this->tasks = new tasks();
$this->tasks->test();
$this->showPanel();
6

public $tasks;
$this->tasks = new tasks();
$this->tasks->test();
$this->showPanel();
7

public $tasks;
$this->tasks = new tasks();
$this->tasks->test();
$this->showPanel();
8

Ví dụ trên sẽ xuất ra:

Ví dụ #15 Độ phân giải tên lớp:

Ghi chú:

Độ phân giải tên lớp sử dụng

$tasks = new Tasks($this);
$tasks->test();
8 là chuyển đổi thời gian biên dịch. Điều đó có nghĩa là tại thời điểm chuỗi tên lớp được tạo chưa có tự động tải đã xảy ra. Kết quả là, tên lớp được mở rộng ngay cả khi lớp không tồn tại. Không có lỗi được ban hành trong trường hợp đó.

$this is defined (A)

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 27
$this is not defined.

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.

Deprecated: Non-static method B::bar() should not be called statically in %s  on line 32

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.
0

Ví dụ trên sẽ xuất ra:

Ví dụ #16 Thiếu độ phân giải tên lớpget_class() on the object.

Kể từ Php 8.0.0, hằng số

$tasks = new Tasks($this);
$tasks->test();
8 cũng có thể được sử dụng trên các đối tượng. Độ phân giải này xảy ra vào thời gian chạy, không phải thời gian biên dịch. Hiệu ứng của nó giống như gọi get_class () trên đối tượng.

$this is defined (A)

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 27
$this is not defined.

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.

Deprecated: Non-static method B::bar() should not be called statically in %s  on line 32

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.
2

Ví dụ trên sẽ xuất ra:

Ví dụ #17 Độ phân giải tên đối tượng

Phương pháp và thuộc tính NULLSAFE

$this is defined (A)

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 27
$this is not defined.

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.

Deprecated: Non-static method B::bar() should not be called statically in %s  on line 32

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.
4 then
$this is defined (A)

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 27
$this is not defined.

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.

Deprecated: Non-static method B::bar() should not be called statically in %s  on line 32

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.
4
will be returned rather than an exception thrown. If the dereference is part of a chain, the rest of the chain is skipped.

Kể từ Php 8.0.0, các thuộc tính và phương thức cũng có thể được truy cập với toán tử "nullsafe" thay thế:

$this is defined (A)

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 27
$this is not defined.

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.

Deprecated: Non-static method B::bar() should not be called statically in %s  on line 32

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.
3. Toán tử nullsafe hoạt động giống như quyền truy cập thuộc tính hoặc phương thức như trên, ngoại trừ nếu đối tượng bị bỏ rơi là
$this is defined (A)

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 27
$this is not defined.

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.

Deprecated: Non-static method B::bar() should not be called statically in %s  on line 32

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.
4 thì
$this is defined (A)

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 27
$this is not defined.

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.

Deprecated: Non-static method B::bar() should not be called statically in %s  on line 32

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.
4 sẽ được trả về thay vì ném ngoại lệ. Nếu độ phân giải là một phần của chuỗi, phần còn lại của chuỗi bị bỏ qua.is_null() check first, but more compact.

Hiệu ứng tương tự như gói mỗi truy cập trong kiểm tra is_null () trước tiên, nhưng nhỏ gọn hơn.

$this is defined (A)

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 27
$this is not defined.

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.

Deprecated: Non-static method B::bar() should not be called statically in %s  on line 32

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.
6

Ví dụ #15 Độ phân giải tên lớp:

Ghi chú:

Độ phân giải tên lớp sử dụng

$tasks = new Tasks($this);
$tasks->test();
8 là chuyển đổi thời gian biên dịch. Điều đó có nghĩa là tại thời điểm chuỗi tên lớp được tạo chưa có tự động tải đã xảy ra. Kết quả là, tên lớp được mở rộng ngay cả khi lớp không tồn tại. Không có lỗi được ban hành trong trường hợp đó.

Ví dụ #16 Thiếu độ phân giải tên lớp

$this is defined (A)

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 27
$this is not defined.

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.

Deprecated: Non-static method B::bar() should not be called statically in %s  on line 32

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.
7

$this is defined (A)

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 27
$this is not defined.

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.

Deprecated: Non-static method B::bar() should not be called statically in %s  on line 32

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.
8

$this is defined (A)

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 27
$this is not defined.

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.

Deprecated: Non-static method B::bar() should not be called statically in %s  on line 32

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.
9

$this is defined (A)

Fatal error: Uncaught Error: Non-static method A::foo() cannot be called statically in %s :27
Stack trace:
#0 {main}
  thrown in %s  on line 27
0

$this is defined (A)

Fatal error: Uncaught Error: Non-static method A::foo() cannot be called statically in %s :27
Stack trace:
#0 {main}
  thrown in %s  on line 27
1

$this is defined (A)

Fatal error: Uncaught Error: Non-static method A::foo() cannot be called statically in %s :27
Stack trace:
#0 {main}
  thrown in %s  on line 27
2

$this is defined (A)

Fatal error: Uncaught Error: Non-static method A::foo() cannot be called statically in %s :27
Stack trace:
#0 {main}
  thrown in %s  on line 27
3

$this is defined (A)

Fatal error: Uncaught Error: Non-static method A::foo() cannot be called statically in %s :27
Stack trace:
#0 {main}
  thrown in %s  on line 27
4

Kể từ Php 8.0.0, hằng số

$tasks = new Tasks($this);
$tasks->test();
8 cũng có thể được sử dụng trên các đối tượng. Độ phân giải này xảy ra vào thời gian chạy, không phải thời gian biên dịch. Hiệu ứng của nó giống như gọi get_class () trên đối tượng.

Ví dụ #17 Độ phân giải tên đối tượng

$this is defined (A)

Fatal error: Uncaught Error: Non-static method A::foo() cannot be called statically in %s :27
Stack trace:
#0 {main}
  thrown in %s  on line 27
5

Phương pháp và thuộc tính NULLSAFE

Kể từ Php 8.0.0, các thuộc tính và phương thức cũng có thể được truy cập với toán tử "nullsafe" thay thế:

$this is defined (A)

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 27
$this is not defined.

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.

Deprecated: Non-static method B::bar() should not be called statically in %s  on line 32

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.
3. Toán tử nullsafe hoạt động giống như quyền truy cập thuộc tính hoặc phương thức như trên, ngoại trừ nếu đối tượng bị bỏ rơi là
$this is defined (A)

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 27
$this is not defined.

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.

Deprecated: Non-static method B::bar() should not be called statically in %s  on line 32

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.
4 thì
$this is defined (A)

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 27
$this is not defined.

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.

Deprecated: Non-static method B::bar() should not be called statically in %s  on line 32

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.
4 sẽ được trả về thay vì ném ngoại lệ. Nếu độ phân giải là một phần của chuỗi, phần còn lại của chuỗi bị bỏ qua.

$this is defined (A)

Fatal error: Uncaught Error: Non-static method A::foo() cannot be called statically in %s :27
Stack trace:
#0 {main}
  thrown in %s  on line 27
6

Hiệu ứng tương tự như gói mỗi truy cập trong kiểm tra is_null () trước tiên, nhưng nhỏ gọn hơn.

Ví dụ #18 Nhà điều hành Nullsafe

$this is defined (A)

Fatal error: Uncaught Error: Non-static method A::foo() cannot be called statically in %s :27
Stack trace:
#0 {main}
  thrown in %s  on line 27
7

$this is defined (A)

Fatal error: Uncaught Error: Non-static method A::foo() cannot be called statically in %s :27
Stack trace:
#0 {main}
  thrown in %s  on line 27
8

$this is defined (A)

Fatal error: Uncaught Error: Non-static method A::foo() cannot be called statically in %s :27
Stack trace:
#0 {main}
  thrown in %s  on line 27
9

object(ClassA)#1 (0) {
}
object(ClassB)#1 (0) {
}
object(ClassC)#1 (0) {
}
object(ClassD)#1 (0) {
}

0

object(ClassA)#1 (0) {
}
object(ClassB)#1 (0) {
}
object(ClassC)#1 (0) {
}
object(ClassD)#1 (0) {
}

1

object(ClassA)#1 (0) {
}
object(ClassB)#1 (0) {
}
object(ClassC)#1 (0) {
}
object(ClassD)#1 (0) {
}

2

object(ClassA)#1 (0) {
}
object(ClassB)#1 (0) {
}
object(ClassC)#1 (0) {
}
object(ClassD)#1 (0) {
}

3

Toán tử nullsafe được sử dụng tốt nhất khi NULL được coi là giá trị hợp lệ và có thể có cho một tài sản hoặc phương thức trả về. Để chỉ ra một lỗi, một ngoại lệ ném là thích hợp hơn.

Aaron tại Thatone Dot Com ¶

object(ClassA)#1 (0) {
}
object(ClassB)#1 (0) {
}
object(ClassC)#1 (0) {
}
object(ClassD)#1 (0) {
}

4

14 năm trước

Ví dụ #16 Thiếu độ phân giải tên lớp

object(ClassA)#1 (0) {
}
object(ClassB)#1 (0) {
}
object(ClassC)#1 (0) {
}
object(ClassD)#1 (0) {
}

5

object(ClassA)#1 (0) {
}
object(ClassB)#1 (0) {
}
object(ClassC)#1 (0) {
}
object(ClassD)#1 (0) {
}

6

object(ClassA)#1 (0) {
}
object(ClassB)#1 (0) {
}
object(ClassC)#1 (0) {
}
object(ClassD)#1 (0) {
}

7

object(ClassA)#1 (0) {
}
object(ClassB)#1 (0) {
}
object(ClassC)#1 (0) {
}
object(ClassD)#1 (0) {
}

8

object(ClassA)#1 (0) {
}
object(ClassB)#1 (0) {
}
object(ClassC)#1 (0) {
}
object(ClassD)#1 (0) {
}

9

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
00

Kể từ Php 8.0.0, hằng số

$tasks = new Tasks($this);
$tasks->test();
8 cũng có thể được sử dụng trên các đối tượng. Độ phân giải này xảy ra vào thời gian chạy, không phải thời gian biên dịch. Hiệu ứng của nó giống như gọi get_class () trên đối tượng.

Ví dụ #17 Độ phân giải tên đối tượng

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
01

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
02

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
03

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
04

$this is defined (A)

Fatal error: Uncaught Error: Non-static method A::foo() cannot be called statically in %s :27
Stack trace:
#0 {main}
  thrown in %s  on line 27
4

Phương pháp và thuộc tính NULLSAFE

Aaron tại Thatone Dot Com ¶

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
06

14 năm trước

Pawel Dot Zimnowodzki tại Gmail Dot Com ¶

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
07

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
08

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
09

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
10

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
11

$this is defined (A)

Fatal error: Uncaught Error: Non-static method A::foo() cannot be called statically in %s :27
Stack trace:
#0 {main}
  thrown in %s  on line 27
4

4 tháng trước

Ví dụ #17 Độ phân giải tên đối tượng

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
13

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
14

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
15

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
16

Phương pháp và thuộc tính NULLSAFE

Ví dụ #17 Độ phân giải tên đối tượng

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
17

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
18

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
19

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
20

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
21

Phương pháp và thuộc tính NULLSAFE

Kể từ Php 8.0.0, các thuộc tính và phương thức cũng có thể được truy cập với toán tử "nullsafe" thay thế:

$this is defined (A)

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 27
$this is not defined.

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.

Deprecated: Non-static method B::bar() should not be called statically in %s  on line 32

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.
3. Toán tử nullsafe hoạt động giống như quyền truy cập thuộc tính hoặc phương thức như trên, ngoại trừ nếu đối tượng bị bỏ rơi là
$this is defined (A)

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 27
$this is not defined.

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.

Deprecated: Non-static method B::bar() should not be called statically in %s  on line 32

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.
4 thì
$this is defined (A)

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 27
$this is not defined.

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.

Deprecated: Non-static method B::bar() should not be called statically in %s  on line 32

Deprecated: Non-static method A::foo() should not be called statically in %s  on line 20
$this is not defined.
4 sẽ được trả về thay vì ném ngoại lệ. Nếu độ phân giải là một phần của chuỗi, phần còn lại của chuỗi bị bỏ qua.

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
22

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
23

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
24

$this is defined (A)

Fatal error: Uncaught Error: Non-static method A::foo() cannot be called statically in %s :27
Stack trace:
#0 {main}
  thrown in %s  on line 27
4

Hiệu ứng tương tự như gói mỗi truy cập trong kiểm tra is_null () trước tiên, nhưng nhỏ gọn hơn.

Ví dụ #18 Nhà điều hành Nullsafe

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
26

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
27

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
20

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
29

Làm thế nào có thể sử dụng lớp từ một lớp khác trong PHP?

Trong PHP, các lớp được khai báo với lệnh lớp trong khi các đối tượng được khai báo với từ khóa mới. Kế thừa được khởi tạo bằng cách sử dụng từ khóa mở rộng. Kế thừa là một khái niệm trong PHP OOP, nơi các lớp được tạo ra từ một lớp khác. Nói một cách đơn giản, nó có nghĩa là để lấy một lớp từ một lớp khác.classes are declared with the class command while objects are declared with the new keyword. Inheritance is initialized using the extends keyword. Inheritance is a concept in PHP OOP, where classes are created out of another class. Simply put, it means to derive one class from another.

Bạn có thể có một lớp học trong một lớp học trong PHP không?

Bằng cách ẩn lớp B trong lớp A, các thành viên của A có thể được tuyên bố riêng tư và B có thể truy cập chúng.Ngoài ra, B tự nó có thể được ẩn khỏi thế giới bên ngoài.Các lớp lồng nhau có thể dẫn đến mã dễ đọc và có thể duy trì hơn.. In addition, B itself can be hidden from the outside world. Nested classes can lead to more readable and maintainable code.

Lớp PHP có thể mở rộng hai lớp không?

Các lớp, lớp trường hợp, đối tượng và đặc điểm đều có thể mở rộng không nhiều hơn một lớp nhưng có thể mở rộng nhiều đặc điểm cùng một lúc.can all extend no more than one class but can extend multiple traits at the same time.

Làm thế nào để bạn gọi một lớp trong PHP?

PHP: Các hằng số lớp Khi gọi một lớp hằng số bằng cách sử dụng cú pháp $ glassName :: hằng số, tên lớp thực sự có thể là một biến.Kể từ Php 5.3, bạn có thể truy cập hằng số lớp tĩnh bằng cách sử dụng tham chiếu biến (ví dụ: className :: $ varconstant).$classname :: constant syntax, the classname can actually be a variable. As of PHP 5.3, you can access a static class constant using a variable reference (Example: className :: $varConstant).