Skip to content

Immutable classes with pure constructor and same constant args as single object  #754

@mkornaukhov

Description

@mkornaukhov

In case of

$a = new A(CONST_ONE, CONST_TWO);
$b = new A(CONST_ONE, CONST_TWO);

we want

  1. Constructor of class A with parameters CONST_ONE, CONST_TWO to be called once and transpiled as global variable that is initialized only once
  2. Each invocation of new A(CONST_ONE, CONST_TWO) to be replaced with such a variable

It would allow use some constructions such as

const c = new A(CONST_ONE, CONST_TWO);

with the following restrictions on class A:

  • Marked as @kphp-immutable-class
  • Has constructor that marked as @kphp-pure-function
  • CONST_ONE, CONST_TWO are constant expressions

It would possibly improve performance and definitely help to implement enums in a natural way.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestoptimizationMemory comsumption / CPU speedup

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions