Skip to content

Commit 6096208

Browse files
committed
stop using dynamic properties in the Context object
Signed-off-by: Demin Yin <deminy@deminy.net>
1 parent 5c9faf1 commit 6096208

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

examples/csp/context.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@
1818
* docker compose exec -t client bash -c "./csp/context.php"
1919
*
2020
* The output should look like the following:
21-
* Name of the 1st object in the Context object of coroutine #1: co1_obj1
22-
* Name of the 2nd object in the Context object of coroutine #2: co2_obj2
21+
* Name of the object in the Context object of coroutine #1: co1_obj
22+
* Name of the object in the Context object of coroutine #2: co2_obj
2323
*
2424
* Coroutine #2 is exiting. The context object of it will be destroyed.
25-
* Object "co2_obj2" is destroyed.
26-
* Object "co2_obj1" is destroyed.
25+
* Object "co2_obj" is destroyed.
2726
*
2827
* Coroutine #1 is exiting. The context object of it will be destroyed.
29-
* Object "co1_obj2" is destroyed.
30-
* Object "co1_obj1" is destroyed.
28+
* Object "co1_obj" is destroyed.
3129
*/
3230

3331
use Swoole\Coroutine;
@@ -48,19 +46,22 @@ public function __destruct()
4846
}
4947
};
5048

49+
// Prior to PHP 8.2, items in the Context object of a coroutine could be set or accessed using dynamic properties,
50+
// e.g.,
51+
// Coroutine::getContext($cid)->foo = 'bar';
52+
// Starting with PHP 8.2, dynamic properties are deprecated, so we use array-style access instead.
53+
5154
// The Context object of a coroutine works the same as an \ArrayObject object.
52-
Coroutine::getContext()['co1_obj1'] = new $class('co1_obj1'); // @phpstan-ignore offsetAccess.nonOffsetAccessible
53-
$cid = Coroutine::getCid(); // Coroutine::getCid() returns 1.
54-
Coroutine::getContext($cid)->co1_obj2 = new $class('co1_obj2'); // @phpstan-ignore property.nonObject
55+
Coroutine::getContext()['co1_obj'] = new $class('co1_obj'); // @phpstan-ignore offsetAccess.nonOffsetAccessible
56+
$cid = Coroutine::getCid(); // Coroutine::getCid() returns 1.
5557

5658
Coroutine::create(function () use ($class) {
5759
// The Context object of a coroutine works the same as an \ArrayObject object.
58-
Coroutine::getContext()['co2_obj1'] = new $class('co2_obj1'); // @phpstan-ignore offsetAccess.nonOffsetAccessible
59-
$cid = Coroutine::getCid(); // Coroutine::getCid() returns 2.
60-
Coroutine::getContext($cid)->co2_obj2 = new $class('co2_obj2'); // @phpstan-ignore property.nonObject
60+
Coroutine::getContext()['co2_obj'] = new $class('co2_obj'); // @phpstan-ignore offsetAccess.nonOffsetAccessible
61+
$cid = Coroutine::getCid(); // Coroutine::getCid() returns 2.
6162

62-
echo 'Name of the 1st object in the Context object of coroutine #1: ', Coroutine::getContext(1)['co1_obj1']->name, PHP_EOL; // @phpstan-ignore-line
63-
echo 'Name of the 2nd object in the Context object of coroutine #2: ', Coroutine::getContext(2)->co2_obj2->name, PHP_EOL; // @phpstan-ignore property.nonObject
63+
echo 'Name of the object in the Context object of coroutine #1: ', Coroutine::getContext(1)['co1_obj']->name, PHP_EOL; // @phpstan-ignore-line
64+
echo 'Name of the object in the Context object of coroutine #2: ', Coroutine::getContext(2)['co2_obj']->name, PHP_EOL; // @phpstan-ignore property.nonObject
6465

6566
echo PHP_EOL, 'Coroutine #2 is exiting. The context object of it will be destroyed.', PHP_EOL;
6667
});

0 commit comments

Comments
 (0)