Craft\StatePersister » CStatePersister » CApplicationComponent » CComponent
IApplicationComponent, IStatePersister

StatePersister implements a file-based persistent data storage.

It can be used to keep data available through multiple requests and sessions.

By default, StatePersister stores data in a file named 'state.bin' that is located {@link PathService::getStatePath()}.

To retrieve the data from StatePersister, call {@link load()}. To save the data, call {@link save()}.

A comparison among state persister, session and cache is as follows:

  • Session: data persisting within a single user session.
  • State Persister: data persisting through all requests/sessions (e.g. hit counter).
  • Cache: volatile and fast storage. It may be used as storage medium for session or state persister.

Since server resource is often limited, be cautious if you plan to use StatePersister to store large amount of data. You should also consider using database-based persister to improve the throughput.

StatePersister is a core application component used to store global application state. It may be accessed via {@link WebApp::getStatePersister()}.

Craft overrides the default {@link \CStatePersister} so it can set a custom path at runtime for our state file.

See also

View source

Public Properties

$behaviorsarray – The behaviors that should be attached to this component.
$cacheIDstring – The ID of the cache application component that is used to cache the state values.
$stateFilestring – The file path storing the state data.

Public Methods

__call()Calls the named method which is not a class method.
__get()Returns a property value, an event handler list or a behavior based on its name.
__isset()Checks if a property value is null.
__set()Sets value of a component property.
__unset()Sets a component property to be null.
asa()Returns the named behavior object.
attachBehavior()Attaches a behavior to this component.
attachBehaviors()Attaches a list of behaviors to the component.
attachEventHandler()Attaches an event handler to an event.
canGetProperty()Determines whether a property can be read.
canSetProperty()Determines whether a property can be set.
detachBehavior()Detaches a behavior from the component.
detachBehaviors()Detaches all behaviors from the component.
detachEventHandler()Detaches an existing event handler.
disableBehavior()Disables an attached behavior.
disableBehaviors()Disables all behaviors attached to this component.
enableBehavior()Enables an attached behavior.
enableBehaviors()Enables all behaviors attached to this component.
evaluateExpression()Evaluates a PHP expression or callback under the context of this component.
getEventHandlers()Returns the list of attached event handlers for an event.
getIsInitialized()Checks if this application component has been initialized.
hasEvent()Determines whether an event is defined.
hasEventHandler()Checks whether the named event has attached handlers.
hasProperty()Determines whether a property is defined.
init()Initializes the component.
load()Loads state data from persistent storage.
raiseEvent()Raises an event.
save()Saves application state in persistent storage.


Initializes the component.

View source




public null init ( )


Saves application state in persistent storage.

View source


  • $state (mixed) – The state data (must be serializable).


public void save ( $state )

Protected Methods

getContent()Loads content from file using a shared lock to avoid data corruption when reading the file while it is being written by save()