xp-framework/rfc

XP8

thekid opened this issue ยท 6 comments

Scope of Change

This RFC defines what will be in XP8.

Rationale

Document features, changes and migration.

Functionality

XP8 will be the first framework release to be PHP7 only. For a feature overview of PHP7, see https://wiki.php.net/rfc#php_70

Return types

Return types will be adopted.

Scalar type hints

Scalar type hints will be added.

Anonymous class support

PHP7 supports a new class() { ... } syntax - see https://wiki.php.net/rfc/anonymous_classes - which can replace our newinstance() core functionality. Example:

$ git diff
diff --git a/.../StringOfTest.class.php b/.../StringOfTest.class.php
index c83da31..6424cc6 100644
--- a/src/test/php/net/xp_framework/unittest/core/StringOfTest.class.php
+++ b/src/test/php/net/xp_framework/unittest/core/StringOfTest.class.php
@@ -18,9 +18,9 @@ class StringOfTest extends \unittest\TestCase {
    * @return lang.Object
    */
   protected function testStringInstance() {
-    return newinstance(Object::class, [], [
-      'toString' => function() { return 'TestString(6) { String }'; }
-    ]);
+    return new class() extends Object {
+      public function toString() { return 'TestString(6) { String }'; }
+    };
   }

Null-coalesce ??

Code will be rewritten where applicable, greatly improving readability:

diff --git a/src/main/php/util/Properties.class.php b/src/main/php/util/Properties.class.php
index 53746cb..a16a69f 100755
--- a/src/main/php/util/Properties.class.php
+++ b/src/main/php/util/Properties.class.php
@@ -284,10 +284,7 @@ class Properties extends \lang\Object implements PropertyAccess {
    */
   public function readSection($name, $default= []) {
     $this->_load();
-    return isset($this->_data[$name])
-      ? $this->_data[$name]
-      : $default
-    ;
+    return $this->_data[$name] ?? $default;
   }

Comparison <=>.

Code can be rewritten to use the "spaceship" operator.

Closure::call

Code in various places can benefit from https://wiki.php.net/rfc/closure_apply

Group use

Coding standards will be update to include https://wiki.php.net/rfc/group_use_declarations

Throwable

Places catching \Exception and \Throwable to ensure PHP 5 and PHP 7 compatibility can be rewritten to only catch the latter.

Context sensitive lexer

Methods can now use keywords - see https://wiki.php.net/rfc/context_sensitive_lexer. This can be used in the framework and libraries.

Unicode

Code can benefit from the added \u{xxxxx} Unicode Codepoint Escape Syntax.

Security considerations

n/a

Speed impact

Faster when replacing newinstance() with native anonymous class support

Dependencies

PHP 7.0

Related documents

http://www.php.net/ChangeLog-7.php

We can use new class(...) extends self inside enums if php/php-src#1779 is merged.

Example:

abstract class Operation extends \lang\Enum {
  public static $plus, $minus, $times, $dividedBy;

  static function __static() {
    self::$plus= new class(0, 'plus') extends self() {
      static function __static() { }
      public function apply($a, $b) { return $a + $b; }
    }
    // ...
  }

  public abstract function apply($a, $b);
}

Rewrote code...

  • ...to use anonymous classes.
  • ...to use ??
  • ...to use <=>
  • ...to use grouped use statements
  • ...and replaced all \Exception (PHP5) handling with \Throwable (PHP 7)

Compact Apidoc Syntax variation

When using (primitive) parameter and return type hints, the apidoc tags feel unnecessary. For examle:

class T {

  /**
   * Creates a string representation
   *
   * @return string
   */
  public function toString() {
    return 'T';
  }
}

RFC #284 already introduced compact apidoc syntax by only adding /** @return string */ as doc comment and omitting the description. This is superfluent in XP8, because the string type can be used as syntax. For XP8, I suggest we re-add the doc comment again, but let it contain the description. It does feel funny completely without!

class T {

  /** Creates a string representation */
  public function toString(): string { return 'T'; }
}

โœ… Added PHP 7.1 to Travis-CI in xp-framework/core@c3b78cd

Libraries to support XP8

xp-framework

xp-forge

:shipit: XP8 release