Skip to content

[Feature request] Add class cache syntax #10139

@Nuranto

Description

@Nuranto

Description

It happens a lot to use class property as a caching method.

For example :

class OrderStatus {
      protected $statusLabels = [];

      public function getStatusLabel(int $statusCode) {
              if(!isset($this->statusLabels[$statusCode])) {
                      $this->statusLabels[$statusCode] = $this->loadStatusLabelFromDatabase($statusCode);
              }
              return $this->statusLabels[$statusCode];
      }
}

Would be great to have some new syntax to do this behind the scene :

class OrderStatus {
      public cacheable function getStatusLabel(int $statusCode) {
              return $this->loadStatusLabelFromDatabase($statusCode);
      }
}

Of course, some rules should exists, like a function cannot be cached if some parameters are not typed or not a basic type, or dynamic, since this feature requires to serialize method parameters.

What do you think ?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions