-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[RFC] Property hooks #13455
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
Merged
[RFC] Property hooks #13455
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
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
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
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
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 was deleted.
Oops, something went wrong.
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,22 @@ | ||
--TEST-- | ||
Abstract hooks compile successfully | ||
--FILE-- | ||
<?php | ||
|
||
abstract class A { | ||
public abstract $prop { | ||
get; | ||
set {} | ||
} | ||
} | ||
|
||
class B extends A { | ||
public $prop { | ||
get {} | ||
} | ||
} | ||
|
||
?> | ||
===DONE=== | ||
--EXPECT-- | ||
===DONE=== |
15 changes: 15 additions & 0 deletions
15
Zend/tests/property_hooks/abstract_hook_in_non_abstract_class.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,15 @@ | ||
--TEST-- | ||
Abstract hooks in non-abstract class gives an error | ||
--FILE-- | ||
<?php | ||
|
||
class Test { | ||
public abstract $prop { | ||
get; | ||
set {} | ||
} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Class Test contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Test::$prop::get) in %s on line %d |
17 changes: 17 additions & 0 deletions
17
Zend/tests/property_hooks/abstract_hook_not_implemented.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,17 @@ | ||
--TEST-- | ||
Abstract hooks that are not implemented throw an error | ||
--FILE-- | ||
<?php | ||
|
||
abstract class A { | ||
public abstract $prop { | ||
get; | ||
set {} | ||
} | ||
} | ||
|
||
class B extends A {} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Class B contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (A::$prop::get) in %s on line %d |
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,17 @@ | ||
--TEST-- | ||
Explicit hooked property satisfies abstract property | ||
--FILE-- | ||
<?php | ||
|
||
abstract class A { | ||
abstract public $prop { get; set; } | ||
} | ||
|
||
class B extends A { | ||
public $prop { get {} set {} } | ||
} | ||
|
||
?> | ||
===DONE=== | ||
--EXPECT-- | ||
===DONE=== |
14 changes: 14 additions & 0 deletions
14
Zend/tests/property_hooks/abstract_prop_not_implemented.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,14 @@ | ||
--TEST-- | ||
Abstract property not implemented throws an error | ||
--FILE-- | ||
<?php | ||
|
||
class A { | ||
abstract public $prop { get; set; } | ||
} | ||
|
||
class B extends A {} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Class A contains 2 abstract methods and must therefore be declared abstract or implement the remaining methods (A::$prop::get, A::$prop::set) in %s on line %d |
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,17 @@ | ||
--TEST-- | ||
Plain property satisfies abstract property | ||
--FILE-- | ||
<?php | ||
|
||
abstract class A { | ||
abstract public $prop { get; set; } | ||
} | ||
|
||
class B extends A { | ||
public $prop; | ||
} | ||
|
||
?> | ||
===DONE=== | ||
--EXPECT-- | ||
===DONE=== |
12 changes: 12 additions & 0 deletions
12
Zend/tests/property_hooks/abstract_prop_without_hooks.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,12 @@ | ||
--TEST-- | ||
Abstract property without hook is illegal | ||
--FILE-- | ||
<?php | ||
|
||
class C { | ||
abstract public $prop; | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Only hooked properties may be declared abstract in %s on line %d |
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,49 @@ | ||
--TEST-- | ||
Array offset on ArrayAccess object in virtual property is allowed | ||
--FILE-- | ||
<?php | ||
|
||
class Collection implements ArrayAccess { | ||
public function offsetExists(mixed $offset): bool { | ||
echo __METHOD__ . "\n"; | ||
return true; | ||
} | ||
|
||
public function offsetGet(mixed $offset): mixed { | ||
echo __METHOD__ . "\n"; | ||
return true; | ||
} | ||
|
||
public function offsetSet(mixed $offset, mixed $value): void { | ||
echo __METHOD__ . "\n"; | ||
} | ||
|
||
public function offsetUnset(mixed $offset): void { | ||
echo __METHOD__ . "\n"; | ||
} | ||
} | ||
|
||
class C { | ||
public function __construct( | ||
public Collection $collection = new Collection(), | ||
) {} | ||
public $prop { | ||
get => $this->collection; | ||
} | ||
} | ||
|
||
$c = new C(); | ||
var_dump($c->prop['foo']); | ||
var_dump($c->prop[] = 'foo'); | ||
var_dump(isset($c->prop['foo'])); | ||
unset($c->prop['foo']); | ||
|
||
?> | ||
--EXPECT-- | ||
Collection::offsetGet | ||
bool(true) | ||
Collection::offsetSet | ||
string(3) "foo" | ||
Collection::offsetExists | ||
bool(true) | ||
Collection::offsetUnset |
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,45 @@ | ||
--TEST-- | ||
Hook AST printing | ||
--FILE-- | ||
<?php | ||
|
||
try { | ||
assert(false && new class { | ||
public $prop1 { get; set; } | ||
public $prop2 { | ||
get { | ||
return parent::$prop1::get(); | ||
} | ||
final set { | ||
echo 'Foo'; | ||
$this->prop1 = 42; | ||
} | ||
} | ||
public $prop3 = 1 { | ||
get => 42; | ||
} | ||
}); | ||
} catch (Error $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
assert(false && new class { | ||
public $prop1 { | ||
get; | ||
set; | ||
} | ||
public $prop2 { | ||
get { | ||
return parent::$prop1::get(); | ||
} | ||
final set { | ||
echo 'Foo'; | ||
$this->prop1 = 42; | ||
} | ||
} | ||
public $prop3 = 1 { | ||
get => 42; | ||
} | ||
}) |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since hooks never receive by ref, we may be able to optimize ops to their non-ref variants like we do for known functions. E.g. for this code:
we generate this:
and it should optimize to something like this once the optimizer knows that hooks do not receive by ref:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. Parent hooks are not very optimized at this moment. I think they will be rare in practice. I'll have a look nonetheless. We might be switching to a different syntax though (
parent::$prop
andparent::$prop = $value
), if possible.