Php 简明教程
PHP – Deprecated Features
随着每个新版本的添加一些新功能,一些功能也会被移除,因为它们被认为已过时。在本章中,我们将了解 PHP 版本 5 之后的弃用功能。
As some new features are added with each new version, some features are also removed as they are deemed to be obsolete. In this chapter, we have a look at deprecated features after PHP version 5.
Deprecated in PHP Ver 7
PHP 4 Style Constructors
PHP 4 样式的构造函数是与它们定义所在的类同名的函数,现在已弃用,并且将在未来移除。如果 PHP 4 构造函数是类中定义的唯一构造函数,则 PHP 7 将发出 E_DEPRECATED。实现 __construct() 函数的类不受影响。
PHP 4 style Constructors are methods having same name as the class they are defined in, are now deprecated, and will be removed in the future. PHP 7 will emit E_DEPRECATED if a PHP 4 constructor is the only constructor defined within a class. Classes implementing a __construct() method are unaffected.
Example
请看以下示例:
Take a look at the following example −
<?php
class A {
function A() {
print('Style Constructor');
}
}
?>
它在浏览器中生成以下 output −
It produces the following output on the browser −
Deprecated: Methods with the same name as their class will not be
constructors in a future version of PHP; A has a deprecated constructor in...
Static Calls to Non-static Methods
对非静态方法的静态调用已弃用,并且将来可能会被移除。
Static calls to non-static methods are deprecated, and may be removed in the future.
Example
请看以下示例:
Take a look at the following example −
<?php
class A {
function b() {
print('Non-static call');
}
}
A::b();
?>
它在浏览器中生成以下 output −
It produces the following output on the browser −
Deprecated: Non-static method A::b() should not be called statically in...
Non-static call
password_hash() salt option
password_hash() 函数的 salt 选项已弃用,以便开发人员不会生成他们自己的(通常不安全的)salt。当开发人员不提供 salt 时,函数本身会生成一个加密安全的 salt - 因此不再需要自定义 salt 生成了。
The salt option for the password_hash() function has been deprecated so that the developers do not generate their own (usually insecure) salts. The function itself generates a cryptographically secure salt, when no salt is provided by the developer - thus custom salt generation is not required any more.
capture_session_meta SSL context option
capture_session_meta SSL 上下文选项已弃用。SSL 元数据现在通过 stream_get_meta_data() 函数使用。
The capture_session_meta SSL context option has been deprecated. SSL metadata is now used through the stream_get_meta_data() function.
ext/mcrypt
mcrypt 扩展已弃用,取而代之的是 OpenSSL。
The mcrypt extension has been deprecated in favour of OpenSSL.
Unquoted Strings
不存在全局常量的未引用的字符串将被视为它们自身的字符串。此行为过去会发出 E_NOTICE,但现在会发出 E_WARNING。在下一个 PHP 主要版本中,将转而引发 Error 异常。
Unquoted strings that are non-existent global constants are taken to be strings of themselves. This behaviour used to emit an E_NOTICE, but will now emit an E_WARNING. In the next major version of PHP, an Error exception will be thrown instead.
The __autoload() Method
__autoload() 函数已弃用,因为它不如 spl_autoload_register() (因为它无法链接自动加载器),并且这两种自动加载样式之间没有互操作性。
The __autoload() method has been deprecated because it is inferior to spl_autoload_register() (due to it not being able to chain autoloaders), and there is no interoperability between the two autoloading styles.
The create_function() Function
鉴于此函数存在安全问题,现在已弃用。首选的替代方法是使用匿名函数。
Given the security issues of this function has now been deprecated. The preferred alternative is to use anonymous functions.
The each() Function
此函数会给一些语言更改带来实现问题。因此已将其弃用。
This function causes implementation issues for some language changes. It has therefore been deprecated.
Case-Insensitive Constants
声明不区分大小写的常数已被弃用。现在传递 true 作为 define() 的第三个参数将生成弃用警告。
The declaration of case-insensitive constants has been deprecated. Passing true as the third argument to define() will now generate a deprecation warning.
Deprecated in PHP Ver 8
如果具有默认值的参数后面跟随一个必需参数,则默认值无效。自 PHP 8.0.0 起,则此操作已被弃用,并且通常可以通过删除默认值来解决,而无需更改功能 −
If a parameter with a default value is followed by a required parameter, the default value has no effect. This is deprecated as of PHP 8.0.0 and can generally be resolved by dropping the default value, without a change in functionality −
<?php
function test($a = [], $b) {} // Before
function test($a, $b) {} // After
?>
此规则的一个例外是形式为 Type $param = null 的参数,其中 null 默认值使类型隐式可为 null。这种用法仍然允许,但建议改为使用显式可为 null 的类型 −
One exception to this rule are parameters of the form Type $param = null, where the null default makes the type implicitly nullable. This usage remains allowed, but it is recommended to use an explicit nullable type instead −
<?php
function test(A $a = null, $b) {} // Still allowed
function test(?A $a, $b) {} // Recommended
?>
显式将 exclude_disabled 设置为 false 调用 get_defined_functions() 已被弃用,并且不再有效。get_defined_functions() 永远不会包含已禁用的函数。
Calling get_defined_functions() with exclude_disabled explicitly set to false is deprecated and no longer has an effect. get_defined_functions() will never include disabled functions.
现在,返回 true 或 false 的排序比较函数将抛出弃用警告,并且应该用返回小于、等于或大于零的整数的实现来替换。
Sort comparison functions that return true or false will now throw a deprecation warning, and should be replaced with an implementation that returns an integer less than, equal to, or greater than zero.
<?php
// Replace
usort($array, fn($a, $b) => $a > $b);
// With
usort($array, fn($a, $b) => $a <=> $b);
?>
Implicit Incompatible float to int Conversions
导致精度损失的浮点数到整数的隐式转换现在已弃用。这会影响数组键、强制模式下的 int 类型声明以及对整数进行操作的操作符。
The implicit conversion of float to int which leads to a loss in precision is now deprecated. This affects array keys, int type declarations in coercive mode, and operators working on ints.
Calling a Static Element on a Trait
直接在特性上调用静态方法或访问静态属性已弃用。只有在使用特性的类上才应访问静态方法和属性。
Calling a static method, or accessing a static property directly on a trait is deprecated. Static methods and properties should only be accessed on a class using the trait.
Date Functions
已弃用 date_sunrise() 和 date_sunset()。请改为使用 date_sun_info()。
date_sunrise() and date_sunset() have been deprecated. Use date_sun_info() instead.
已弃用 strptime()。请改为使用 date_parse_from_format()(用于与区域设置无关的解析)或 IntlDateFormatter::parse()(用于与区域设置相关的解析)。
strptime() has been deprecated. Use date_parse_from_format() instead (for locale-independent parsing), or IntlDateFormatter::parse() (for locale-dependent parsing).
已弃用 strftime() 和 gmstrftime()。您可以改为使用 date()(用于与区域设置无关的格式化)或 IntlDateFormatter::format()(用于与区域设置相关的格式化)。
strftime() and gmstrftime() have been deprecated. You can use date() instead (for locale-independent formatting), or IntlDateFormatter::format() (for locale-dependent formatting).