Hi,

Am 04.08.25 um 17:23 schrieb Larry Garfield:
Hi folks.  I recently wrote a blog post on behalf of the PHP Foundation about 
some work Gina has been doing regarding generics.  It's a follow-up to last 
year's post on a similar topic.  We are especially looking for feedback from 
the Internals crew, and voters in particular, on whether or not to proceed.

https://thephp.foundation/blog/2025/08/05/compile-generics/

Wearing my Internals/voter hat, my stance is a very strong "yes please!"

Thanks for that nice write-up and potentially great improvement.


Can you please elaborate why it wouldn't be possible to create "empty extending classes" during compile-time of a file when there is a "`new` generic" found?


Some example to make it clearer:


Given a generic class


  ```
  namespace SomeNamespace;

  use Some\Entity;

  abstract class MyGenericClass<T: Entity>
  {
      // ...
  }
  ```


and given an entity


  ```
  namespace MyEntities;

  use Some\Entity;

  class UserEntity extends Entity
  {
      // ...
  }
  ```


when using a "`new` generic" in some file


  ```
  namespace OtherNamespace;

  use SomeNamespace\MyGenericClass;
  use MyEntities\UserEntity;

  $myGenericObject = new MyGenericClass<UserEntity>;
  ```


and when compiling that file


then the compiler creates an "empty extending class" on the fly if there is none available in the OpCache (from compiling some other file with the same "`new` generic") that has code equivalent to

  ```
  namespace SomeNamespace;

  use MyEntities\UserEntity;

  class MyGenericClass<UserEntity> extends MyGenericClass<Entity>
  {}
  ```


That simply(TM) means, to allow `<` and `>` in class names but only internally to the engine and if generated by the compiler itself.


Further question: Would it be possible to automatically mark a class as abstract if it is a generic class?


Probably you have thought about this option and I just don't understand anything about internals of PHP. So don't hesitate to just answer very shortly. ;-)


Best regards
Thomas

Reply via email to