-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Fix GH-12695: skip __get() when __isset() materialised the property #22181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
iluuu1994
merged 1 commit into
php:master
from
nicolas-grekas:gh12695-isset-materialization-fix
Jul 20, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| --TEST-- | ||
| GH-12695: ?? on unset property does not call __get() when __isset() materialized the property | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| #[AllowDynamicProperties] | ||
| class A { | ||
| public function __get($n) { | ||
| throw new Exception("__get must not be called when __isset materialised the property"); | ||
| } | ||
| public function __isset($n) { | ||
| echo " __isset($n)\n"; | ||
| $this->$n = 123; | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| echo "Dynamic property materialised in __isset, then `??`:\n"; | ||
| $a = new A; | ||
| var_dump($a->foo ?? 'fallback'); | ||
|
|
||
| echo "\nSame on a declared (unset) property:\n"; | ||
| class B { | ||
| public int $x = 99; | ||
| public function __get($n) { | ||
| throw new Exception("__get must not be called when __isset materialised the property"); | ||
| } | ||
| public function __isset($n) { | ||
| echo " __isset($n)\n"; | ||
| $this->$n = 7; | ||
| return true; | ||
| } | ||
| } | ||
| $b = new B; | ||
| unset($b->x); | ||
| var_dump($b->x ?? 'fallback'); | ||
|
|
||
| echo "\nWhen __isset() materialises the property to null, `??` falls back:\n"; | ||
| #[AllowDynamicProperties] | ||
| class D { | ||
| public function __get($n) { | ||
| throw new Exception("__get must not be called when __isset materialised the property"); | ||
| } | ||
| public function __isset($n) { | ||
| echo " __isset($n)\n"; | ||
| $this->$n = null; | ||
| return true; | ||
| } | ||
| } | ||
| $d = new D; | ||
| var_dump($d->foo ?? 'fallback'); | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| Dynamic property materialised in __isset, then `??`: | ||
| __isset(foo) | ||
| int(123) | ||
|
|
||
| Same on a declared (unset) property: | ||
| __isset(x) | ||
| int(7) | ||
|
|
||
| When __isset() materialises the property to null, `??` falls back: | ||
| __isset(foo) | ||
| string(8) "fallback" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| --TEST-- | ||
| GH-12695: empty() on unset property does not call __get() when __isset() materialized the property | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| #[AllowDynamicProperties] | ||
| class A { | ||
| public function __get($n) { | ||
| throw new Exception("__get must not be called when __isset materialised the property"); | ||
| } | ||
| public function __isset($n) { | ||
| echo " __isset($n)\n"; | ||
| $this->$n = $GLOBALS['next_value']; | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| echo "empty() when __isset materialised a truthy value: __get is not called, empty=false:\n"; | ||
| $GLOBALS['next_value'] = 7; | ||
| $a = new A; | ||
| var_dump(empty($a->foo)); | ||
|
|
||
| echo "\nempty() when __isset materialised a falsy value: __get is not called, empty=true:\n"; | ||
| $GLOBALS['next_value'] = 0; | ||
| $a = new A; | ||
| var_dump(empty($a->bar)); | ||
|
|
||
| echo "\nempty() with no materialization: __get is still called (legacy path preserved):\n"; | ||
| class B { | ||
| public function __get($n) { | ||
| echo " __get($n)\n"; | ||
| return 'value'; | ||
| } | ||
| public function __isset($n) { | ||
| echo " __isset($n)\n"; | ||
| return true; | ||
| } | ||
| } | ||
| $b = new B; | ||
| var_dump(empty($b->any)); | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| empty() when __isset materialised a truthy value: __get is not called, empty=false: | ||
| __isset(foo) | ||
| bool(false) | ||
|
|
||
| empty() when __isset materialised a falsy value: __get is not called, empty=true: | ||
| __isset(bar) | ||
| bool(true) | ||
|
|
||
| empty() with no materialization: __get is still called (legacy path preserved): | ||
| __isset(any) | ||
| __get(any) | ||
| bool(false) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| --TEST-- | ||
| GH-12695: __get() invocation is based on __isset()'s return value | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| /* The re-check after __isset() must not affect the legacy path when | ||
| * the property is genuinely magic-only: __get() is still called and | ||
| * its return value drives `??`'s null check. */ | ||
|
|
||
| echo "`??` when __isset=true and __get returns a value: __get is called:\n"; | ||
| class C { | ||
| public function __get($n) { | ||
| echo " __get($n)\n"; | ||
| return 'from-get'; | ||
| } | ||
| public function __isset($n) { | ||
| echo " __isset($n)\n"; | ||
| return true; | ||
| } | ||
| } | ||
| $c = new C; | ||
| var_dump($c->any ?? 'fallback'); | ||
|
|
||
| echo "\n`??` when __isset=true and __get returns null: __get is called and fallback is used:\n"; | ||
| class D { | ||
| public function __get($n) { | ||
| echo " __get($n)\n"; | ||
| return null; | ||
| } | ||
| public function __isset($n) { | ||
| echo " __isset($n)\n"; | ||
| return true; | ||
| } | ||
| } | ||
| $d = new D; | ||
| var_dump($d->any ?? 'fallback'); | ||
|
|
||
| echo "\n`??` when __isset returns false: __get is not called:\n"; | ||
| class E { | ||
| public function __get($n) { | ||
| throw new Exception("__get must not be called when __isset returned false"); | ||
| } | ||
| public function __isset($n) { | ||
| echo " __isset($n)\n"; | ||
| return false; | ||
| } | ||
| } | ||
| $e = new E; | ||
| var_dump($e->any ?? 'fallback'); | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| `??` when __isset=true and __get returns a value: __get is called: | ||
| __isset(any) | ||
| __get(any) | ||
| string(8) "from-get" | ||
|
|
||
| `??` when __isset=true and __get returns null: __get is called and fallback is used: | ||
| __isset(any) | ||
| __get(any) | ||
| string(8) "fallback" | ||
|
|
||
| `??` when __isset returns false: __get is not called: | ||
| __isset(any) | ||
| string(8) "fallback" |
28 changes: 28 additions & 0 deletions
28
Zend/tests/magic_methods/gh12695_object_released_in_isset.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| --TEST-- | ||
| GH-12695: Object freed by __isset() during materialization | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| /* The re-check after __isset() copies the materialised value into the | ||
| * caller's return-value buffer before releasing the object. This is | ||
| * required because __isset() may drop the last external reference to | ||
| * the object (here via $obj = null), so the property table is freed | ||
| * by OBJ_RELEASE() right after the re-check. */ | ||
|
|
||
| class C { | ||
| public $prop; | ||
| public function __isset($name) { | ||
| global $obj; | ||
| $obj = null; | ||
| $this->prop = 'materialised'; | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| $obj = new C(); | ||
| unset($obj->prop); | ||
| var_dump($obj->prop ?? 'fb'); | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| string(12) "materialised" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.