MarkE Posted November 20, 2021 Share Posted November 20, 2021 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 -> 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 More sharing options...
kongondo Posted November 21, 2021 Share Posted November 21, 2021 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/ ? 1 Link to comment Share on other sites More sharing options...
MarkE Posted November 21, 2021 Author Share Posted November 21, 2021 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 More sharing options...
MarkE Posted November 21, 2021 Author Share Posted November 21, 2021 Doh! Thanks for making me think @kongondo. Re-reading the docs, it is clear that there should be no difference between -> and get(). That is, unless you have inadvertantly declared a property with the same name as a data item..... ? 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now