|
| 1 | +--TEST-- |
| 2 | +Test that PHP 8.4 property read/write hooks work as expected on copied classes |
| 3 | +--SKIPIF-- |
| 4 | +<?php if(PHP_VERSION_ID < 80400) die("skip PHP 8.4+ only"); ?> |
| 5 | +--FILE-- |
| 6 | +<?php |
| 7 | + |
| 8 | +class PropertyHooks{ |
| 9 | + |
| 10 | + private int $virtualBackingAsymmetric = 0; |
| 11 | + |
| 12 | + public int $virtualOnlyGet { |
| 13 | + get => $this->virtualBackingAsymmetric + 1; |
| 14 | + //can't be set because it's not backed |
| 15 | + } |
| 16 | + |
| 17 | + public int $virtualOnlySet { |
| 18 | + //do NOT use an arrow function for this - it will make the property non-virtual |
| 19 | + //thanks for the new footgun PHP!!! |
| 20 | + set { $this->virtualBackingAsymmetric = $value - 1; } |
| 21 | + } |
| 22 | + |
| 23 | + private int $virtualBacking = 0; |
| 24 | + |
| 25 | + public int $virtualReadWrite { |
| 26 | + get => $this->virtualBacking + 1; |
| 27 | + set => $this->virtualBacking = $value - 1; |
| 28 | + } |
| 29 | + |
| 30 | + public int $backedOnlyGet { |
| 31 | + get => $this->backedOnlyGet + 1; |
| 32 | + //set will use default property write behaviour |
| 33 | + } |
| 34 | + |
| 35 | + public int $backedOnlySet { |
| 36 | + set => $value + 1; |
| 37 | + } |
| 38 | + |
| 39 | + public int $backedGetSet { |
| 40 | + get => $this->backedGetSet + 1; |
| 41 | + set => $value - 1; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +function test(PropertyHooks $object) : void{ |
| 46 | + var_dump($object); |
| 47 | + |
| 48 | + try{ |
| 49 | + $object->virtualOnlyGet = 1; //error |
| 50 | + echo "Something is wrong, this is not supposed to be writable\n"; |
| 51 | + }catch(\Error $e){ |
| 52 | + echo $e->getMessage() . "\n"; |
| 53 | + } |
| 54 | + |
| 55 | + //TODO: test more stuff |
| 56 | +} |
| 57 | + |
| 58 | +echo "--- main thread test ---\n"; |
| 59 | +test(new PropertyHooks()); |
| 60 | +echo "--- main thread done ---\n"; |
| 61 | + |
| 62 | +$t = new class extends \pmmp\thread\Thread{ |
| 63 | + public function run() : void{ |
| 64 | + echo "--- child thread test ---\n"; |
| 65 | + test(new PropertyHooks()); |
| 66 | + echo "--- child thread done ---\n"; |
| 67 | + } |
| 68 | +}; |
| 69 | +$t->start(\pmmp\thread\Thread::INHERIT_ALL) && $t->join(); |
| 70 | +echo "done\n"; |
| 71 | +--EXPECTF-- |
| 72 | +--- main thread test --- |
| 73 | +object(PropertyHooks)#%d (2) { |
| 74 | + ["virtualBackingAsymmetric":"PropertyHooks":private]=> |
| 75 | + int(0) |
| 76 | + ["virtualBacking":"PropertyHooks":private]=> |
| 77 | + int(0) |
| 78 | + ["virtualReadWrite"]=> |
| 79 | + uninitialized(int) |
| 80 | + ["backedOnlyGet"]=> |
| 81 | + uninitialized(int) |
| 82 | + ["backedOnlySet"]=> |
| 83 | + uninitialized(int) |
| 84 | + ["backedGetSet"]=> |
| 85 | + uninitialized(int) |
| 86 | +} |
| 87 | +Property PropertyHooks::$virtualOnlyGet is read-only |
| 88 | +--- main thread done --- |
| 89 | +--- child thread test --- |
| 90 | +object(PropertyHooks)#%d (2) { |
| 91 | + ["virtualBackingAsymmetric":"PropertyHooks":private]=> |
| 92 | + int(0) |
| 93 | + ["virtualBacking":"PropertyHooks":private]=> |
| 94 | + int(0) |
| 95 | + ["virtualReadWrite"]=> |
| 96 | + uninitialized(int) |
| 97 | + ["backedOnlyGet"]=> |
| 98 | + uninitialized(int) |
| 99 | + ["backedOnlySet"]=> |
| 100 | + uninitialized(int) |
| 101 | + ["backedGetSet"]=> |
| 102 | + uninitialized(int) |
| 103 | +} |
| 104 | +Property PropertyHooks::$virtualOnlyGet is read-only |
| 105 | +--- child thread done --- |
| 106 | +done |
0 commit comments