编程

PHP 8.3: 某些 PHP 扩展类中的类常量类型声明

912 2023-10-23 15:38:00

作为对 PHP 8.3 中支持类常量类型的 PHP 的后续更改,Phar、SNMP 和 Phar 扩展类常量将使用常量类型进行更新。

任何扩展以下类并覆盖现有常量的用户空间 PHP 类都将导致致命错误。

扩展这些类的用户空间 PHP 类并重写常量的情况并不常见。

受影响的扩展:

  • Phar 扩展中的 \Phar
  • SNMP 扩展中的  \SNMP 类 
  • Zip 扩展中的 \ZipArchive

Phar 扩展中类常量类型

Phar 扩展声明了一个 Phar 类,该类声明了多个类常量。在 PHP 8.3 中, Phar 类常量也使用了类型声明。

  class Phar extends RecursiveDirectoryIterator implements Countable, ArrayAccess {

-    const BZ2 = UNKNOWN;
+    const int BZ2 = UNKNOWN;

-    const GZ = UNKNOWN;
+    const int GZ = UNKNOWN;

-    const NONE = UNKNOWN;
+    const int NONE = UNKNOWN;

-    const PHAR = UNKNOWN;
+    const int PHAR = UNKNOWN;

-    const TAR = UNKNOWN;
+    const int TAR = UNKNOWN;

-    const ZIP = UNKNOWN;
+    const int ZIP = UNKNOWN;

-    const COMPRESSED = UNKNOWN;
+    const int COMPRESSED = UNKNOWN;

-    const PHP = UNKNOWN;
+    const int PHP = UNKNOWN;

-    const PHPS = UNKNOWN;
+    const int PHPS = UNKNOWN;

-    const MD5 = UNKNOWN;
+    const int MD5 = UNKNOWN;

-    const OPENSSL = UNKNOWN;
+    const int OPENSSL = UNKNOWN;

-    const OPENSSL_SHA256 = UNKNOWN;
+    const int OPENSSL_SHA256 = UNKNOWN;

-    const OPENSSL_SHA512 = UNKNOWN;
+    const int OPENSSL_SHA512 = UNKNOWN;

-    const SHA1 = UNKNOWN;
+    const int SHA1 = UNKNOWN;

-    const SHA256 = UNKNOWN;
+    const int SHA256 = UNKNOWN;

-    const SHA512 = UNKNOWN;
+    const int SHA512 = UNKNOWN;

    // Rest of the Phar class
  }

SNMP 扩展类常量类型

class SNMP {

-    public const VERSION_1 = UNKNOWN;
+    public const int VERSION_1 = UNKNOWN;

-    public const VERSION_2c = UNKNOWN;
+    public const int VERSION_2c = UNKNOWN;

-    public const VERSION_2C = UNKNOWN;
+    public const int VERSION_2C = UNKNOWN;

-    public const VERSION_3 = UNKNOWN;
+    public const int VERSION_3 = UNKNOWN;

-    public const ERRNO_NOERROR = UNKNOWN;
+    public const int ERRNO_NOERROR = UNKNOWN;

-    public const ERRNO_ANY = UNKNOWN;
+    public const int ERRNO_ANY = UNKNOWN;

-    public const ERRNO_GENERIC = UNKNOWN;
+    public const int ERRNO_GENERIC = UNKNOWN;

-    public const ERRNO_TIMEOUT = UNKNOWN;
+    public const int ERRNO_TIMEOUT = UNKNOWN;

-    public const ERRNO_ERROR_IN_REPLY = UNKNOWN;
+    public const int ERRNO_ERROR_IN_REPLY = UNKNOWN;

-    public const ERRNO_OID_NOT_INCREASING = UNKNOWN;
+    public const int ERRNO_OID_NOT_INCREASING = UNKNOWN;

-    public const ERRNO_OID_PARSING_ERROR = UNKNOWN;
+    public const int ERRNO_OID_PARSING_ERROR = UNKNOWN;

-    public const ERRNO_MULTIPLE_SET_QUERIES = UNKNOWN;
+    public const int ERRNO_MULTIPLE_SET_QUERIES = UNKNOWN;
      // Rest of the SNMP class
  }

Zip 扩展类常量类型

Zip 扩展中的 ZipArchive 也为它类中的常量声明了类型。

  class ZipArchive implements Countable {

-    public const CREATE = UNKNOWN;
+    public const int CREATE = UNKNOWN;

-    public const EXCL = UNKNOWN;
+    public const int EXCL = UNKNOWN;

-    public const CHECKCONS = UNKNOWN;
+    public const int CHECKCONS = UNKNOWN;

-    public const OVERWRITE = UNKNOWN;
+    public const int OVERWRITE = UNKNOWN;

-    public const RDONLY = UNKNOWN;
+    public const int RDONLY = UNKNOWN;

-    public const FL_NOCASE = UNKNOWN;
+    public const int FL_NOCASE = UNKNOWN;

-    public const FL_NODIR = UNKNOWN;
+    public const int FL_NODIR = UNKNOWN;

-    public const FL_COMPRESSED = UNKNOWN;
+    public const int FL_COMPRESSED = UNKNOWN;

-    public const FL_UNCHANGED = UNKNOWN;
+    public const int FL_UNCHANGED = UNKNOWN;

-    public const FL_RECOMPRESS = UNKNOWN;
+    public const int FL_RECOMPRESS = UNKNOWN;

-    public const FL_ENCRYPTED = UNKNOWN;
+    public const int FL_ENCRYPTED = UNKNOWN;

-    public const FL_OVERWRITE = UNKNOWN;
+    public const int FL_OVERWRITE = UNKNOWN;

-    public const FL_LOCAL = UNKNOWN;
+    public const int FL_LOCAL = UNKNOWN;

-    public const FL_CENTRAL = UNKNOWN;
+    public const int FL_CENTRAL = UNKNOWN;

-    public const FL_ENC_GUESS = UNKNOWN;
+    public const int FL_ENC_GUESS = UNKNOWN;

-    public const FL_ENC_RAW = UNKNOWN;
+    public const int FL_ENC_RAW = UNKNOWN;

-    public const FL_ENC_STRICT = UNKNOWN;
+    public const int FL_ENC_STRICT = UNKNOWN;

-    public const FL_ENC_UTF_8 = UNKNOWN;
+    public const int FL_ENC_UTF_8 = UNKNOWN;

-    public const FL_ENC_CP437 = UNKNOWN;
+    public const int FL_ENC_CP437 = UNKNOWN;

-    public const FL_OPEN_FILE_NOW = UNKNOWN;
+    public const int FL_OPEN_FILE_NOW = UNKNOWN;

-    public const CM_DEFAULT = UNKNOWN;
+    public const int CM_DEFAULT = UNKNOWN;

-    public const CM_STORE = UNKNOWN;
+    public const int CM_STORE = UNKNOWN;

-    public const CM_SHRINK = UNKNOWN;
+    public const int CM_SHRINK = UNKNOWN;

-    public const CM_REDUCE_1 = UNKNOWN;
+    public const int CM_REDUCE_1 = UNKNOWN;

-    public const CM_REDUCE_2 = UNKNOWN;
+    public const int CM_REDUCE_2 = UNKNOWN;

-    public const CM_REDUCE_3 = UNKNOWN;
+    public const int CM_REDUCE_3 = UNKNOWN;

-    public const CM_REDUCE_4 = UNKNOWN;
+    public const int CM_REDUCE_4 = UNKNOWN;

-    public const CM_IMPLODE = UNKNOWN;
+    public const int CM_IMPLODE = UNKNOWN;

-    public const CM_DEFLATE = UNKNOWN;
+    public const int CM_DEFLATE = UNKNOWN;

-    public const CM_DEFLATE64 = UNKNOWN;
+    public const int CM_DEFLATE64 = UNKNOWN;

-    public const CM_PKWARE_IMPLODE = UNKNOWN;
+    public const int CM_PKWARE_IMPLODE = UNKNOWN;

-    public const CM_BZIP2 = UNKNOWN;
+    public const int CM_BZIP2 = UNKNOWN;

-    public const CM_LZMA = UNKNOWN;
+    public const int CM_LZMA = UNKNOWN;

-    public const CM_LZMA2 = UNKNOWN;
+    public const int CM_LZMA2 = UNKNOWN;

-    public const CM_ZSTD = UNKNOWN;
+    public const int CM_ZSTD = UNKNOWN;

-    public const CM_XZ = UNKNOWN;
+    public const int CM_XZ = UNKNOWN;

-    public const CM_TERSE = UNKNOWN;
+    public const int CM_TERSE = UNKNOWN;

-    public const CM_LZ77 = UNKNOWN;
+    public const int CM_LZ77 = UNKNOWN;

-    public const CM_WAVPACK = UNKNOWN;
+    public const int CM_WAVPACK = UNKNOWN;

-    public const CM_PPMD = UNKNOWN;
+    public const int CM_PPMD = UNKNOWN;

-    public const ER_OK = UNKNOWN;
+    public const int ER_OK = UNKNOWN;

-    public const ER_MULTIDISK = UNKNOWN;
+    public const int ER_MULTIDISK = UNKNOWN;

-    public const ER_RENAME = UNKNOWN;
+    public const int ER_RENAME = UNKNOWN;

-    public const ER_CLOSE = UNKNOWN;
+    public const int ER_CLOSE = UNKNOWN;

-    public const ER_SEEK = UNKNOWN;
+    public const int ER_SEEK = UNKNOWN;

-    public const ER_READ = UNKNOWN;
+    public const int ER_READ = UNKNOWN;

-    public const ER_WRITE = UNKNOWN;
+    public const int ER_WRITE = UNKNOWN;

-    public const ER_CRC = UNKNOWN;
+    public const int ER_CRC = UNKNOWN;

-    public const ER_ZIPCLOSED = UNKNOWN;
+    public const int ER_ZIPCLOSED = UNKNOWN;

-    public const ER_NOENT = UNKNOWN;
+    public const int ER_NOENT = UNKNOWN;

-    public const ER_EXISTS = UNKNOWN;
+    public const int ER_EXISTS = UNKNOWN;

-    public const ER_OPEN = UNKNOWN;
+    public const int ER_OPEN = UNKNOWN;

-    public const ER_TMPOPEN = UNKNOWN;
+    public const int ER_TMPOPEN = UNKNOWN;

-    public const ER_ZLIB = UNKNOWN;
+    public const int ER_ZLIB = UNKNOWN;

-    public const ER_MEMORY = UNKNOWN;
+    public const int ER_MEMORY = UNKNOWN;

-    public const ER_CHANGED = UNKNOWN;
+    public const int ER_CHANGED = UNKNOWN;

-    public const ER_COMPNOTSUPP = UNKNOWN;
+    public const int ER_COMPNOTSUPP = UNKNOWN;

-    public const ER_EOF = UNKNOWN;
+    public const int ER_EOF = UNKNOWN;

-    public const ER_INVAL = UNKNOWN;
+    public const int ER_INVAL = UNKNOWN;

-    public const ER_NOZIP = UNKNOWN;
+    public const int ER_NOZIP = UNKNOWN;

-    public const ER_INTERNAL = UNKNOWN;
+    public const int ER_INTERNAL = UNKNOWN;

-    public const ER_INCONS = UNKNOWN;
+    public const int ER_INCONS = UNKNOWN;

-    public const ER_REMOVE = UNKNOWN;
+    public const int ER_REMOVE = UNKNOWN;

-    public const ER_DELETED = UNKNOWN;
+    public const int ER_DELETED = UNKNOWN;

-    public const ER_ENCRNOTSUPP = UNKNOWN;
+    public const int ER_ENCRNOTSUPP = UNKNOWN;

-    public const ER_RDONLY = UNKNOWN;
+    public const int ER_RDONLY = UNKNOWN;

-    public const ER_NOPASSWD = UNKNOWN;
+    public const int ER_NOPASSWD = UNKNOWN;

-    public const ER_WRONGPASSWD = UNKNOWN;
+    public const int ER_WRONGPASSWD = UNKNOWN;

-    public const ER_OPNOTSUPP = UNKNOWN;
+    public const int ER_OPNOTSUPP = UNKNOWN;

-    public const ER_INUSE = UNKNOWN;
+    public const int ER_INUSE = UNKNOWN;

-    public const ER_TELL = UNKNOWN;
+    public const int ER_TELL = UNKNOWN;

-    public const ER_COMPRESSED_DATA = UNKNOWN;
+    public const int ER_COMPRESSED_DATA = UNKNOWN;

-    public const ER_CANCELLED = UNKNOWN;
+    public const int ER_CANCELLED = UNKNOWN;

-    public const ER_DATA_LENGTH = UNKNOWN;
+    public const int ER_DATA_LENGTH = UNKNOWN;

-    public const ER_NOT_ALLOWED = UNKNOWN;
+    public const int ER_NOT_ALLOWED = UNKNOWN;

-    public const AFL_RDONLY = UNKNOWN;
+    public const int AFL_RDONLY = UNKNOWN;

-    public const AFL_IS_TORRENTZIP = UNKNOWN;
+    public const int AFL_IS_TORRENTZIP = UNKNOWN;

-    public const AFL_WANT_TORRENTZIP = UNKNOWN;
+    public const int AFL_WANT_TORRENTZIP = UNKNOWN;

-    public const AFL_CREATE_OR_KEEP_FILE_FOR_EMPTY_ARCHIVE = UNKNOWN;
+    public const int AFL_CREATE_OR_KEEP_FILE_FOR_EMPTY_ARCHIVE = UNKNOWN;

-    public const OPSYS_DOS = UNKNOWN;
+    public const int OPSYS_DOS = UNKNOWN;

-    public const OPSYS_AMIGA = UNKNOWN;
+    public const int OPSYS_AMIGA = UNKNOWN;

-    public const OPSYS_OPENVMS = UNKNOWN;
+    public const int OPSYS_OPENVMS = UNKNOWN;

-    public const OPSYS_UNIX = UNKNOWN;
+    public const int OPSYS_UNIX = UNKNOWN;

-    public const OPSYS_VM_CMS = UNKNOWN;
+    public const int OPSYS_VM_CMS = UNKNOWN;

-    public const OPSYS_ATARI_ST = UNKNOWN;
+    public const int OPSYS_ATARI_ST = UNKNOWN;

-    public const OPSYS_OS_2 = UNKNOWN;
+    public const int OPSYS_OS_2 = UNKNOWN;

-    public const OPSYS_MACINTOSH = UNKNOWN;
+    public const int OPSYS_MACINTOSH = UNKNOWN;

-    public const OPSYS_Z_SYSTEM = UNKNOWN;
+    public const int OPSYS_Z_SYSTEM = UNKNOWN;

-    public const OPSYS_CPM = UNKNOWN;
+    public const int OPSYS_CPM = UNKNOWN;

-    public const OPSYS_WINDOWS_NTFS = UNKNOWN;
+    public const int OPSYS_WINDOWS_NTFS = UNKNOWN;

-    public const OPSYS_MVS = UNKNOWN;
+    public const int OPSYS_MVS = UNKNOWN;

-    public const OPSYS_VSE = UNKNOWN;
+    public const int OPSYS_VSE = UNKNOWN;

-    public const OPSYS_ACORN_RISC = UNKNOWN;
+    public const int OPSYS_ACORN_RISC = UNKNOWN;

-    public const OPSYS_VFAT = UNKNOWN;
+    public const int OPSYS_VFAT = UNKNOWN;

-    public const OPSYS_ALTERNATE_MVS = UNKNOWN;
+    public const int OPSYS_ALTERNATE_MVS = UNKNOWN;

-    public const OPSYS_BEOS = UNKNOWN;
+    public const int OPSYS_BEOS = UNKNOWN;

-    public const OPSYS_TANDEM = UNKNOWN;
+    public const int OPSYS_TANDEM = UNKNOWN;

-    public const OPSYS_OS_400 = UNKNOWN;
+    public const int OPSYS_OS_400 = UNKNOWN;

-    public const OPSYS_OS_X = UNKNOWN;
+    public const int OPSYS_OS_X = UNKNOWN;

-    public const OPSYS_DEFAULT = UNKNOWN;
+    public const int OPSYS_DEFAULT = UNKNOWN;

-    public const EM_NONE = UNKNOWN;
+    public const int EM_NONE = UNKNOWN;

-    public const EM_TRAD_PKWARE = UNKNOWN;
+    public const int EM_TRAD_PKWARE = UNKNOWN;

-    public const EM_AES_128 = UNKNOWN;
+    public const int EM_AES_128 = UNKNOWN;

-    public const EM_AES_192 = UNKNOWN;
+    public const int EM_AES_192 = UNKNOWN;

-    public const EM_AES_256 = UNKNOWN;
+    public const int EM_AES_256 = UNKNOWN;

-    public const EM_UNKNOWN = UNKNOWN;
+    public const int EM_UNKNOWN = UNKNOWN;

-    public const LIBZIP_VERSION = UNKNOWN;
+    public const string LIBZIP_VERSION = UNKNOWN;

-    public const LENGTH_TO_END = UNKNOWN;
+    public const int LENGTH_TO_END = UNKNOWN;

-    public const LENGTH_UNCHECKED = UNKNOWN;
+    public const int LENGTH_UNCHECKED = UNKNOWN;-+

    // Rest of the ZipArchive class
  }

向后兼容性影响

这是 PHP 8.3 中引入的破坏性变更。不过,用户空间的 PHP 应用通常不会集成这些类或者重写这些常量。

不过,如果用户空间的 PHP 类中继承了这些类,并且对现有常量名进行了常量声明,新的类常量声明必须包含类型,并且必须是可兼容的。

class ParentClass {
    public const string|int VALUE = 'MyValue';
}
class ChildClass extends ParentClass {
    public const VALUE = 'MyValue';
}
Fatal error: Type of ChildClass::VALUE must be compatible with ParentClass::VALUE of type string|int