WireData
The base data-storage class used throughout ProcessWire
Extends Wire with a built-in
key-value store: properties are held in a protected $data array and exposed through
get() and set() methods, direct property access, and array access — all interchangeable.
Wire is the base class for Page, Field, Template, many Module classes, and many other ProcessWire objects, so its API is available on many of the objects you work with.
You can also extend it directly to create lightweight data-container classes, or use it
standalone as a simple key-value store.
Wire is not itself an API variable. Access it as the base of the objects you already
use ($page, $field, $template, etc.), or create a standalone instance directly:
// Equivalent ways to create a new WireData instance
$item = new WireData();
$item = WireData(); // global shortcut function 3.0.126+
$item = WireData(['color' => 'blue', 'n' => 3]); // shortcut with initial data All three access styles are equivalent and fully interchangeable for normal stored values:
// Set
$item->set('color', 'blue');
$item->color = 'blue';
$item['color'] = 'blue';
// Get
$color = $item->get('color');
$color = $item->color;
$color = $item['color'];When a value is missing or explicitly null, get() and property access return
null, while array access returns false.
set($key, $value)
Sets a named property. Participates in change tracking if enabled (see below).
Returns $this for chaining.
$item->set('title', 'Hello')->set('status', 1);setQuietly($key, $value)
Same as set() but bypasses change tracking. Useful when updating internal state
that should not be considered a user-initiated change.
$item->setQuietly('_cache', $computedValue);setArray(array $data)
Sets multiple properties at once from an associative array.
$item->setArray([
'title' => 'Hello',
'color' => 'blue',
'weight' => 42,
]);get($key)
Returns the value of a property, or null if not set. Also falls through to API
variable lookup (via the parent Wire class) when the key is not in $data.
$value = $item->get('color'); // 'blue', or null if not setPipe syntax — first non-empty value:
Pass multiple property names separated by | and get() returns the first
non-empty value it finds. Useful as a fallback chain:
// Return localTitle if set, otherwise fall back to title
$label = $item->get('localTitle|title');has($key)
Returns true if the property exists and its value is non-null. Because get()
falls through to API-variable lookup, has() can also return true for available
API variable names.
if($item->has('color')) {
echo $item->color;
}getArray()
Returns the full $data array as an associative array.
$all = $item->getArray(); // ['title' => 'Hello', 'color' => 'blue', ...]getDot() retrieves a value using dot-separated key chains, traversing nested
Wire or WireArray objects automatically.
// Equivalent to $page->get('parent')->get('title')
$parentTitle = $page->getDot('parent.title');
// Three levels deep
$value = $item->getDot('a.b.c');Dot syntax is used internally by Page::get(), so $page->get('parent.title')
already works on Page objects without calling getDot() directly.
Iterating a Wire object yields all properties in $data as key-value pairs:
foreach($item as $key => $value) {
echo "$key: $value\n";
}remove($key)
Removes a property. Participates in change tracking.
$item->remove('color');
unset($item->color); // equivalent
unset($item['color']); // equivalentremoveQuietly($key) — 3.0.262+
Removes a property without change tracking.
$item->removeQuietly('_cache');data() reads and writes directly from/to the $data array, bypassing any extra
logic a subclass may have added to get() or set(). Useful in subclasses or
when you need to work with raw stored values.
// Get a single value (bypasses overridden get())
$raw = $item->data('color');
// Set a single value (bypasses overridden set())
$item->data('color', 'red');
// Get the entire $data array
$all = $item->data();
// Replace the entire $data array
$item->data(['title' => 'Hello', 'color' => 'blue'], true);
// Merge into $data (without true, it merges rather than replaces)
$item->data(['weight' => 42]);and() combines the current object with another item or collection and returns a
new WireArray. Useful for applying filters or checks across a set of items that
don't yet exist in a single collection:
// Does this page or any of its parents have featured=1?
if($page->and($page->parents)->has("featured=1")) {
// ...
}
// Combine with a WireArray
$combined = $item->and($otherCollection);
// Wrap this item alone in a WireArray
$array = $item->and();When change tracking is enabled (it is by default on Page and other saveable
objects), set() and remove() record which properties changed since the last
save. setQuietly() and removeQuietly() bypass this.
$item->setTrackChanges(true);
$item->set('color', 'red'); // tracked
$item->setQuietly('color', 'red'); // not tracked
$changed = $item->getChanges(); // ['color']Change tracking is inherited from Wire. The full API: trackChanges(),
setTrackChanges(), getChanges(), and resetTrackChanges().
- Source file:
wire/core/WireData/Wire Data.php - Extends:
Wire— all hook and API-variable access fromWireis available - Implements:
IteratorAggregate,ArrayAccess - Common subclasses:
Page,Field,Template,Module,WireInputData,HookEvent, and many other ProcessWire objects - Extending Wire
Data: subclasses typically overrideget()and/orset()to add type coercion, lazy loading, or other logic; usedata()to access the raw array from within those overrides dataas a key: calling$item->set('data', $array)is equivalent to$item->setArray($array)— it merges the given array into$datarather than storing it under the key'data'- Pipe syntax limitation:
get('a|b')returns the first non-empty (truthy) value, not merely the first non-null one — a value of0or''is skipped
Wire Properties set to a Wire Click any linked item for full usage details and examples. Hookable methods are indicated with the icon. In addition to those shown below, the Take the current item and append the given item(s), returning a new WireArray Get or set a low-level data value Retrieve the value for a previously set property, or retrieve an API variable Returns the full array of properties set to this object Get a property via dot syntax: field.subfield.subfield Enables the object data properties to be iterable as an array Does this object have the given property with a non-null value? Get or set a low-level data value Remove a previously set property Quietly remove a previously set property (no change tracking) Set a value to this object’s data Set an array of key=value pairs Same as set() but without change tracking In addition to the methods and properties above, WireWire class with the fundamental difference being that it is designed
for runtime data storage. It provides this primarily through the built-in get() and set() methods for
getting and setting named properties to WirePage, the type used for all pages in ProcessWire. $item->property or using array access like $item[$property]. If you foreach() a WireWire class also inherits all the methods and properties of: Wire.Retrieval
Name Return Summary WireArrayarray Wire null mixed null array null mixed ArrayObject bool Manipulation
Name Return Summary array Wire null $this $this $this $this $this Additional methods and properties
API reference based on ProcessWire core version 3.0.264