Skip to main content
added a note about an OOP replacement to drupal_static(), on Drupal 8.6
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

The major difference is that drupal_static() saves values in a PHP static variable (static $data = array(), in that function). As such, it can beis used to store values that are used when serving the samecache data within a single page request. (Normally, drupal_static() is used as cacheit stores the data in a static variable.)

The
The class returned by \Drupal::state() handles values that need to be preserved between requests. As such, it is probable it stores its values in a database table. (It depends from the exact implementation done from that class.)

drupal_static() is faster since it saves its values in the memory, but using states doesn't make a lot of difference.

As for recommending one, both are recommended. It's up to you using the one that suits your needs. Do you need a cross-request value, or a value you use only in one request?

If you are looking to replace drupal_static() with OOP code, Drupal 8.6 introduced the MemoryCache class, that stores cache items in memory using a PHP array. It has been added to allow classes that would previously maintain state on a protected property (as a static cache to avoid persistent cache or database lookups) to inject a service to hold that state instead. (See Added memory cache.)
Notice that the class isn't used directly. The module needing it should define a new service, similarly to what Drupal core does with the entity.memory_cache service.

entity.memory_cache:
  class: Drupal\Core\Cache\MemoryCache\MemoryCache

The major difference is that drupal_static() saves values in a PHP static variable (static $data = array(), in that function). As such, it can be used to store values that are used when serving the same page. (Normally, drupal_static() is used as cache.)

The class returned by \Drupal::state() handles values that need to be preserved between requests. As such, it is probable it stores its values in a database table. (It depends from the exact implementation done from that class.)

drupal_static() is faster since it saves its values in the memory, but using states doesn't make a lot of difference.

As for recommending one, both are recommended. It's up to you using the one that suits your needs. Do you need a cross-request value, or a value you use only in one request?

drupal_static() is used to cache data within a single page request. it stores the data in a static variable.
The class returned by \Drupal::state() handles values that need to be preserved between requests. As such, it is probable it stores its values in a database table. (It depends from the exact implementation done from that class.)

drupal_static() is faster since it saves its values in the memory, but using states doesn't make a lot of difference.

As for recommending one, both are recommended. It's up to you using the one that suits your needs. Do you need a cross-request value, or a value you use only in one request?

If you are looking to replace drupal_static() with OOP code, Drupal 8.6 introduced the MemoryCache class, that stores cache items in memory using a PHP array. It has been added to allow classes that would previously maintain state on a protected property (as a static cache to avoid persistent cache or database lookups) to inject a service to hold that state instead. (See Added memory cache.)
Notice that the class isn't used directly. The module needing it should define a new service, similarly to what Drupal core does with the entity.memory_cache service.

entity.memory_cache:
  class: Drupal\Core\Cache\MemoryCache\MemoryCache
added 2 characters in body
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

The major difference is that drupal_static() saves values in a PHP static variable (static $data = array(), in that function). As such, it can be used to store values that are used when serving the same page. (Normally, drupal_static() is used as cache.)

The class returned by \Drupal::state() handles values that need to be preserved between requests. As such, it is probable it stores its values in a database table. (It depends from the exact implementation done from that class.)

drupal_static()drupal_static() is faster since it saves its values in the memory, but using states doesn't make a lot of difference.

As for recommending one, both are recommended. It's up to you using the one that suits your needs. Do you need a cross-request value, or a value you use only in one request?

The major difference is that drupal_static() saves values in a PHP static variable (static $data = array(), in that function). As such, it can be used to store values that are used when serving the same page. (Normally, drupal_static() is used as cache.)

The class returned by \Drupal::state() handles values that need to be preserved between requests. As such, it is probable it stores its values in a database table. (It depends from the exact implementation done from that class.)

drupal_static() is faster since it saves its values in the memory, but using states doesn't make a lot of difference.

As for recommending one, both are recommended. It's up to you using the one that suits your needs. Do you need a cross-request value, or a value you use only in one request?

The major difference is that drupal_static() saves values in a PHP static variable (static $data = array(), in that function). As such, it can be used to store values that are used when serving the same page. (Normally, drupal_static() is used as cache.)

The class returned by \Drupal::state() handles values that need to be preserved between requests. As such, it is probable it stores its values in a database table. (It depends from the exact implementation done from that class.)

drupal_static() is faster since it saves its values in the memory, but using states doesn't make a lot of difference.

As for recommending one, both are recommended. It's up to you using the one that suits your needs. Do you need a cross-request value, or a value you use only in one request?

Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

The major difference is that drupal_static() saves values in a PHP static variable (static $data = array(), in that function). As such, it can be used to store values that are used when serving the same page. (Normally, drupal_static() is used as cache.)

The class returned by \Drupal::state() handles values that need to be preserved between requests. As such, it is probable it stores its values in a database table. (It depends from the exact implementation done from that class.)

drupal_static() is faster since it saves its values in the memory, but using states doesn't make a lot of difference.

As for recommending one, both are recommended. It's up to you using the one that suits your needs. Do you need a cross-request value, or a value you use only in one request?