By dawehner on
Change record status:
Draft (View all draft change records)
Project:
Introduced in branch:
8.7.x
Issue links:
Description:
drupal_static() and drupal_static_reset() are deprecated and will be removed before Drupal 9.0.0. The reason is that it's not recommended to have any kind of in-memory caches based on the static keyword. It breaks encapsulation, reusability, makes testing hard etc, so don't use that in the first place. An ordinary property on an object can deal with almost the same kind of use-cases.
Before
$foo = &drupal_static::get(__FUNCTION__);
drupal_static_reset(__FUNCTION__);
drupal_static_reset();
After
class Foo {
protected $bar;
public function getBar() {
if (!isset($this->bar) {
$this->bar = // Do your logic here.
}
return $this->bar;
}
}
Impacts:
Module developers
Themers
Site templates, recipes and distribution developers
Comments
It looks like the approach
It looks like the approach being taken in Core is often to leverage the site cache to replace drupal_static() implementations. As such, if you want something to help implement custom caching, check out https://www.drupal.org/project/cache_register.
Deprecation has been postponed to D11
See the original issue: https://www.drupal.org/node/2260187