Jump to content

WireData get/set vs object operators


MarkE
 Share

Recommended Posts

Having used PW for a while now, I'm still confused about when to use get and set for WireData objects and when to just use object operators.

As I understand it, WireData objects have a 'data' property with associated 'get' and 'set' methods:

/**
	 * Array where get/set properties are stored
	 *
	 */
	protected $data = array(); 

	/**
	 * Set a value to this object’s data
	 * 
	 * ~~~~~
	 * // Set a value for a property
	 * $item->set('foo', 'bar');
	 * 
	 * // Set a property value directly
	 * $item->foo = 'bar';
	 * 
	 * // Set a property using array access
	 * $item['foo'] = 'bar';
	 * ~~~~~

It also seems that you can access the data items directly using the object operator ->

WireData.jpg.51ffa4f56851d4be1e048ffdaa697245.jpg

However, I recall situations (which I can't replicate in this simple example) where the object operator does not return the data item. Am I imagining things? Is there any guidance on when to use 'get' and 'set' rather than the operator (which is much easier in the IDE)?

Link to comment
Share on other sites

Quote

WireData is the base data-storage class used by many ProcessWire object types and most modules.

WireData is very much like its parent Wire 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 WireData objects. The most common example of a WireData object is Page, the type used for all pages in ProcessWire.

Properties set to a WireData object can also be set or accessed directly, like $item->property or using array access like $item[$property]. If you foreach() a WireData object, the default behavior is to iterate all of the properties/values present within it.

https://processwire.com/api/ref/wire-data/

?

  • Like 1
Link to comment
Share on other sites

Thanks for that @kongondo. It's just that I thought there may be some extra subleties because on some occasions $object->property did not work, but $object->get('property') did and I couldn't understand why. Unfortunately it is a bit difficult to build an example outside of a rather complex context (my FieldtypeMeasurement module), but I'll see if I can find a way of simply illustrating this behaviour.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...