Skip to content

Commit 72c8746

Browse files
beberleiTimWollailuuu1994
authored
RFC: Add #[\Deprecated] Attribute (#11293)
see https://wiki.php.net/rfc/deprecated_attribute Co-authored-by: Tim Düsterhus <[email protected]> Co-authored-by: Ilija Tovilo <[email protected]>
1 parent 8291e81 commit 72c8746

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1401
-165
lines changed

NEWS

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ PHP NEWS
3232
(Julien Voisin)
3333
. Fixed bug GH-11928 (The --enable-re2c-cgoto doesn't add the -g flag).
3434
(Peter Kokot)
35+
. Added the #[\Deprecated] attribute. (beberlei, timwolla)
3536

3637
- Curl:
3738
. Deprecated the CURLOPT_BINARYTRANSFER constant. (divinity76)

UPGRADING

+2
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ PHP 8.4 UPGRADE NOTES
213213
they allow chaining method calls, property accesses, etc. without enclosing
214214
the expression in parentheses.
215215
RFC: https://wiki.php.net/rfc/new_without_parentheses
216+
. Added the #[\Deprecated] attribute.
217+
RFC: https://wiki.php.net/rfc/deprecated_attribute
216218

217219
- Curl:
218220
. curl_version() returns an additional feature_list value, which is an

Zend/Optimizer/optimize_func_calls.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ static void zend_delete_call_instructions(zend_op_array *op_array, zend_op *opli
7878
static void zend_try_inline_call(zend_op_array *op_array, zend_op *fcall, zend_op *opline, zend_function *func)
7979
{
8080
if (func->type == ZEND_USER_FUNCTION
81-
&& !(func->op_array.fn_flags & (ZEND_ACC_ABSTRACT|ZEND_ACC_HAS_TYPE_HINTS))
81+
&& !(func->op_array.fn_flags & (ZEND_ACC_ABSTRACT|ZEND_ACC_HAS_TYPE_HINTS|ZEND_ACC_DEPRECATED))
8282
/* TODO: function copied from trait may be inconsistent ??? */
8383
&& !(func->op_array.fn_flags & (ZEND_ACC_TRAIT_CLONE))
8484
&& fcall->extended_value >= func->op_array.required_num_args
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--TEST--
2+
#[\Deprecated]: Class Constants.
3+
--FILE--
4+
<?php
5+
6+
class Clazz {
7+
#[\Deprecated]
8+
public const TEST = 1;
9+
10+
#[\Deprecated()]
11+
public const TEST2 = 2;
12+
13+
#[\Deprecated("use Clazz::TEST instead")]
14+
public const TEST3 = 3;
15+
16+
#[\Deprecated]
17+
public const TEST4 = 4;
18+
19+
#[\Deprecated]
20+
public const TEST5 = 5;
21+
}
22+
23+
var_dump(Clazz::TEST);
24+
var_dump(Clazz::TEST2);
25+
var_dump(Clazz::TEST3);
26+
27+
var_dump(constant('Clazz::TEST4'));
28+
var_dump(defined('Clazz::TEST5'));
29+
30+
?>
31+
--EXPECTF--
32+
Deprecated: Constant Clazz::TEST is deprecated in %s on line %d
33+
int(1)
34+
35+
Deprecated: Constant Clazz::TEST2 is deprecated in %s on line %d
36+
int(2)
37+
38+
Deprecated: Constant Clazz::TEST3 is deprecated, use Clazz::TEST instead in %s on line %d
39+
int(3)
40+
41+
Deprecated: Constant Clazz::TEST4 is deprecated in %s on line %d
42+
int(4)
43+
bool(true)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
#[\Deprecated]: Enum Cases.
3+
--FILE--
4+
<?php
5+
6+
enum E {
7+
#[\Deprecated]
8+
case Test;
9+
10+
#[\Deprecated("use E::Test instead")]
11+
case Test2;
12+
}
13+
14+
E::Test;
15+
E::Test2;
16+
17+
?>
18+
--EXPECTF--
19+
Deprecated: Enum case E::Test is deprecated in %s on line %d
20+
21+
Deprecated: Enum case E::Test2 is deprecated, use E::Test instead in %s on line %d
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
#[\Deprecated]: Using the value of a deprecated class constant as the deprecation message.
3+
--FILE--
4+
<?php
5+
6+
class Clazz {
7+
#[\Deprecated(self::TEST)]
8+
public const TEST = "from itself";
9+
10+
#[\Deprecated]
11+
public const TEST2 = "from another";
12+
13+
#[\Deprecated(self::TEST2)]
14+
public const TEST3 = 1;
15+
}
16+
17+
Clazz::TEST;
18+
Clazz::TEST3;
19+
20+
?>
21+
--EXPECTF--
22+
Deprecated: Constant Clazz::TEST is deprecated, from itself in %s on line %d
23+
24+
Deprecated: Constant Clazz::TEST2 is deprecated in %s on line %d
25+
26+
Deprecated: Constant Clazz::TEST3 is deprecated, from another in %s on line %d
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
#[\Deprecated]: Using the value of a deprecated class constant as the deprecation message with a throwing error handler.
3+
--FILE--
4+
<?php
5+
6+
set_error_handler(function (int $errno, string $errstr, ?string $errfile = null, ?int $errline = null) {
7+
throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
8+
});
9+
10+
class Clazz {
11+
#[\Deprecated(self::TEST)]
12+
public const TEST = "from itself";
13+
14+
#[\Deprecated]
15+
public const TEST2 = "from another";
16+
17+
#[\Deprecated(self::TEST2)]
18+
public const TEST3 = 1;
19+
}
20+
21+
try {
22+
Clazz::TEST;
23+
} catch (ErrorException $e) {
24+
echo "Caught: ", $e->getMessage(), PHP_EOL;
25+
}
26+
27+
try {
28+
Clazz::TEST3;
29+
} catch (ErrorException $e) {
30+
echo "Caught: ", $e->getMessage(), PHP_EOL;
31+
}
32+
33+
?>
34+
--EXPECT--
35+
Caught: Constant Clazz::TEST is deprecated, from itself
36+
Caught: Constant Clazz::TEST2 is deprecated
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
#[\Deprecated]: Using the value of a deprecated class constant in a constant expression.
3+
--FILE--
4+
<?php
5+
6+
class Clazz {
7+
#[\Deprecated("prefix")]
8+
public const PREFIX = "prefix";
9+
10+
#[\Deprecated("suffix")]
11+
public const SUFFIX = "suffix";
12+
13+
public const CONSTANT = self::PREFIX . self::SUFFIX;
14+
}
15+
16+
var_dump(Clazz::CONSTANT);
17+
18+
?>
19+
--EXPECTF--
20+
Deprecated: Constant Clazz::PREFIX is deprecated, prefix in %s on line %d
21+
22+
Deprecated: Constant Clazz::SUFFIX is deprecated, suffix in %s on line %d
23+
string(12) "prefixsuffix"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
#[\Deprecated]: Code is E_USER_DEPRECATED for class constants.
3+
--FILE--
4+
<?php
5+
6+
set_error_handler(function (int $errno, string $errstr, ?string $errfile = null, ?int $errline = null) {
7+
var_dump($errno, E_USER_DEPRECATED, $errno === E_USER_DEPRECATED);
8+
});
9+
10+
class Clazz {
11+
#[\Deprecated]
12+
public const TEST = 1;
13+
}
14+
15+
Clazz::TEST;
16+
17+
?>
18+
--EXPECT--
19+
int(16384)
20+
int(16384)
21+
bool(true)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
#[\Deprecated]: Class constant with value unknown at compile time.
3+
--FILE--
4+
<?php
5+
6+
define('SUFFIX', random_int(1, 2) == 1 ? 'a' : 'b');
7+
8+
class Clazz {
9+
#[\Deprecated]
10+
public const CONSTANT = self::class . '-' . SUFFIX;
11+
}
12+
13+
$value = Clazz::CONSTANT;
14+
var_dump($value);
15+
var_dump($value === 'Clazz-' . SUFFIX);
16+
17+
?>
18+
--EXPECTF--
19+
Deprecated: Constant Clazz::CONSTANT is deprecated in %s on line %d
20+
string(7) "Clazz-%c"
21+
bool(true)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
--TEST--
2+
#[\Deprecated]: Functions and Methods.
3+
--FILE--
4+
<?php
5+
6+
#[\Deprecated]
7+
function test() {
8+
}
9+
10+
#[\Deprecated("use test() instead")]
11+
function test2() {
12+
}
13+
14+
class Clazz {
15+
#[\Deprecated]
16+
function test() {
17+
}
18+
19+
#[\Deprecated("use test() instead")]
20+
function test2() {
21+
}
22+
}
23+
24+
$closure = #[\Deprecated] function() {
25+
};
26+
27+
$closure2 = #[\Deprecated] function() {
28+
};
29+
30+
class Constructor {
31+
#[\Deprecated]
32+
public function __construct() {
33+
}
34+
35+
#[\Deprecated]
36+
public function __destruct() {
37+
}
38+
}
39+
40+
test();
41+
test2();
42+
call_user_func("test");
43+
44+
$cls = new Clazz();
45+
$cls->test();
46+
$cls->test2();
47+
48+
call_user_func([$cls, "test"]);
49+
50+
$closure();
51+
52+
$closure2();
53+
54+
new Constructor();
55+
56+
?>
57+
--EXPECTF--
58+
Deprecated: Function test() is deprecated in %s
59+
60+
Deprecated: Function test2() is deprecated, use test() instead in %s on line %d
61+
62+
Deprecated: Function test() is deprecated in %s on line %d
63+
64+
Deprecated: Method Clazz::test() is deprecated in %s
65+
66+
Deprecated: Method Clazz::test2() is deprecated, use test() instead in %s
67+
68+
Deprecated: Method Clazz::test() is deprecated in %s
69+
70+
Deprecated: Function {closure:%s:%d}() is deprecated in %s on line %d
71+
72+
Deprecated: Function {closure:%s:%d}() is deprecated in %s on line %d
73+
74+
Deprecated: Method Constructor::__construct() is deprecated in %s on line %d
75+
76+
Deprecated: Method Constructor::__destruct() is deprecated in %s on line %d
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
#[\Deprecated]: Exception Handler is deprecated.
3+
--FILE--
4+
<?php
5+
6+
#[\Deprecated]
7+
function my_exception_handler($e) {
8+
echo "Handled: ", $e->getMessage(), PHP_EOL;
9+
};
10+
11+
set_exception_handler('my_exception_handler');
12+
13+
throw new \Exception('test');
14+
15+
?>
16+
--EXPECT--
17+
Deprecated: Function my_exception_handler() is deprecated in Unknown on line 0
18+
Handled: test
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
#[\Deprecated]: Exception Handler is deprecated for throwing error handler.
3+
--FILE--
4+
<?php
5+
6+
function my_error_handler(int $errno, string $errstr, ?string $errfile = null, ?int $errline = null) {
7+
throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
8+
}
9+
10+
set_error_handler('my_error_handler');
11+
12+
#[\Deprecated]
13+
function my_exception_handler($e) {
14+
echo "Handled: ", $e->getMessage(), PHP_EOL;
15+
};
16+
17+
set_exception_handler('my_exception_handler');
18+
19+
#[\Deprecated]
20+
function test() {
21+
}
22+
23+
test();
24+
25+
?>
26+
--EXPECTF--
27+
Fatal error: Uncaught ErrorException: Function my_exception_handler() is deprecated in Unknown:0
28+
Stack trace:
29+
#0 [internal function]: my_error_handler(%d, '%s', '%s', %d)
30+
#1 {main}
31+
thrown in Unknown on line 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
#[\Deprecated]: Error Handler is deprecated.
3+
--FILE--
4+
<?php
5+
6+
#[\Deprecated]
7+
function my_error_handler(int $errno, string $errstr, ?string $errfile = null, ?int $errline = null) {
8+
echo $errstr, PHP_EOL;
9+
};
10+
11+
set_error_handler('my_error_handler');
12+
13+
#[\Deprecated]
14+
function test() {
15+
}
16+
17+
test();
18+
19+
?>
20+
--EXPECTF--
21+
Deprecated: Function my_error_handler() is deprecated in %s on line %d
22+
Function test() is deprecated
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
#[\Deprecated]: Code is E_USER_DEPRECATED for functions.
3+
--FILE--
4+
<?php
5+
6+
set_error_handler(function (int $errno, string $errstr, ?string $errfile = null, ?int $errline = null) {
7+
var_dump($errno, E_USER_DEPRECATED, $errno === E_USER_DEPRECATED);
8+
});
9+
10+
#[\Deprecated]
11+
function test() {
12+
}
13+
14+
test();
15+
16+
?>
17+
--EXPECT--
18+
int(16384)
19+
int(16384)
20+
bool(true)

0 commit comments

Comments
 (0)