Php có thể tuần tự hóa

PHP hỗ trợ các hàm

Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
9 và
Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
0 được sử dụng để chuyển đổi các đối tượng lớp, mảng và dữ liệu vô hướng khác sang dạng chuỗi được tuần tự hóa và tạo lại chúng từ chuỗi được tuần tự hóa

Các lớp PHP có thể cung cấp các triển khai tuần tự hóa của riêng chúng. Ví dụ: một lớp có thể loại trừ dữ liệu nhạy cảm nhất định khỏi việc thêm vào chuỗi được tuần tự hóa hoặc một lớp có thể thiết lập lại kết nối đến máy chủ từ xa từ URL máy chủ được lấy từ văn bản được tuần tự hóa

Có ba cách tiếp cận để cung cấp logic tuần tự hóa tùy chỉnh cho các lớp PHP

  • Phương pháp ma thuật
    Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
    1 và
    Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
    2
  • Giao diện
    Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
    3 và các phương thức
    Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
    4 và
    Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
    0 [kể từ PHP 5. 1]
  • Phương pháp ma thuật
    Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
    1 và
    Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
    2 [kể từ PHP 7. 4]

Triển khai các phương pháp

Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
1 và
Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
2 là cách được khuyến nghị, vì nó tránh được một số cạm bẫy với cách tiếp cận
Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
1/
Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
2 và giao diện
Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
3

Trong PHP8. 1, việc triển khai giao diện

Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
3 mà không triển khai các phương thức
Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
1 và
Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
2 không được chấp nhận

class Test implements Serializable{
    public function serialize[] {}
    public function unserialize[$data] {}
}
Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
  • Giao diện
    class Test implements Serializable{
    
        public function __serialize[]: array {}
        public function __unserialize[array $data]: void {}
    
        public function serialize[]: array {}
        public function unserialize[string $data]: void {}
    
    }
    1 không bị ảnh hưởng
  • Kể từ PHP 8. 0, Chữ ký phương pháp ma thuật lớp được thực thi nghiêm ngặt

Trở lại năm 2019, người ta đã đề xuất thêm hai phương pháp ma thuật mới là

Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
1 và
Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
2 vì cả hai phương pháp tiếp cận giao diện
Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
2 +
Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
1 và
Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
3 đều không lý tưởng do tính phức tạp khi triển khai và hành vi có lỗi của chúng

Sự phản đối này trong PHP 8. 1 là phần tiếp theo của thay đổi đó, để ngừng triển khai giao diện

Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
3 mà không có các phương thức ma thuật mới và cuối cùng là xóa giao diện
Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
3 trong PHP 9. 0

Nếu một lớp triển khai cả phương thức giao diện

Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
3 và phương thức ma thuật, thì phương thức ma thuật sẽ được ưu tiên và thông báo không dùng nữa sẽ không được phát ra

Trong PHP7. 4 trở lên, các phương thức

Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
1 và
Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
2 được thực thi trong các cuộc gọi
Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
32/
Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
0, không phải các phương thức
Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
9/
Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
0 từ giao diện
Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
3. Sẽ không có thông báo phản đối trong PHP 8. 1 hoặc

class Test implements Serializable{

    public function __serialize[]: array {}
    public function __unserialize[array $data]: void {}

    public function serialize[]: array {}
    public function unserialize[string $data]: void {}

}

Thay đổi liên quan

  • PHP8. 1.
    Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
    37 không được dùng nữa

Tác động tương thích ngược

Lưu ý rằng thông báo phản đối không được phát ra nếu lớp cũng thực hiện các phương thức

Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
1 và
Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
2

Nếu một lớp triển khai cả phương thức giao diện

Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
3 và phương thức ma thuật, thì phương thức ma thuật sẽ được ưu tiên

Trên các ứng dụng có PHP 7. 4 là phiên bản tối thiểu, có thể bỏ triển khai giao diện

Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
3 và triển khai các phương thức
Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
1 và
Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
2 mới một cách an toàn

Trên các ứng dụng phải hỗ trợ các phiên bản PHP cũ hơn PHP 7. 4, triển khai cả giao diện

Deprecated: Test implements the Serializable interface, which is deprecated. Implement __serialize[] and __unserialize[] instead [or in addition, if support for old PHP versions is necessary] in .. on line ...
3 [sẽ được sử dụng trên PHP

Chủ Đề