-
Notifications
You must be signed in to change notification settings - Fork 0
Compile PHP enums to PHP 7/8 lookalikes, PHP 8.1 native #106
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
Conversation
However, since XP core reflection does not support constant annotations, we cannot access case annotations. This will be covered by the reflection library!
This package is deprecated, see xp-framework/compiler#106
...and with support for arbitrary expressions as initializers (see #104), we can simply refactor code as follows: @@ -3,8 +3,8 @@
use xml\{Tree, Node};
/** CAS service response - in XML and JSON */
-abstract enum ServiceResponse {
- XML {
+abstract class ServiceResponse {
+ public static $XML= new class() extends self {
public function success($user) {
$n= new Node('cas:authenticationSuccess')->withChild(new Node('cas:user', $user['username']));
if (isset($user['attributes'])) {
@@ -29,8 +29,8 @@ abstract enum ServiceResponse {
$response->send($tree->getSource(INDENT_DEFAULT), 'text/xml');
}
- },
- JSON {
+ };
+ public static $JSON= new class() extends self {
public function success($user) {
$success= ['user' => $user['username']];
if (isset($user['attributes'])) { (In this case, the class already had a static factory method and wasn't using |
PHP master build donwloaded from https://github.com/shivammathur/php-builder-windows/actions: # Verify enum support
$ XP_RT=master xp -w 'enum SortOrder { case ASC; case DESC; } return SortOrder::ASC'
SortOrder {
name => "ASC"
}
# Run XP core tests
core $ XP_RT=master xp test src/test/config/unittest/*ini
# ...
♥: 4205/4259 run (54 skipped), 4205 succeeded, 0 failed
Memory used: 22559.28 kB (43149.24 kB peak)
Time taken: 4.005 seconds
# Run XP compiler test
compiler $ XP_RT=master xp test src/test/php
# ...
♥: 563/564 run (1 skipped), 563 succeeded, 0 failed
Memory used: 9119.41 kB (9177.58 kB peak)
Time taken: 0.251 seconds |
Enums
Implement feature request #100. XP Compiler and XP Framework (10.8.0+) now support:
WeekDay::MON
)enum
syntax and compiles it to XP enums, will be deprecated! Its abstract enum feature is unsupported by PHP 8.1 enums, and may be reintroduced later via the tagged unions RFC.Behind the scenes
What the user writes (and what will be emitted for PHP 8.1 once the enum PR is merged):
What is emitted for PHP < 8.1:
Backed enums also include a value member as well as from() and tryFrom() methods.