Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 <tim@tideways-gmbh.com> Co-authored-by: Ilija Tovilo <ilija.tovilo@me.com>
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‎

Lines changed: 1 addition & 0 deletions
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‎

Lines changed: 2 additions & 0 deletions
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‎

Lines changed: 1 addition & 1 deletion
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
Lines changed: 43 additions & 0 deletions
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)
Lines changed: 21 additions & 0 deletions
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
Lines changed: 26 additions & 0 deletions
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
Lines changed: 36 additions & 0 deletions
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
Lines changed: 23 additions & 0 deletions
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"
Lines changed: 21 additions & 0 deletions
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)
Lines changed: 21 additions & 0 deletions
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)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /