5.2. Further Explorations

PHOCOA Key-Value Coding and Bindings Primer

PHOCOA bindings make it easy to show information from your Key-Value Coding Compliant objects, which includes Propel objects if you've implemented the Propel changes noted in the appendix.

You can use WFObject's valueForKeyPath() function to access properties of your objects. For instance, if you have a Book object, and want to access the birthdate of the author, you could use:

$authorBirthDate = $book->valueForKeyPath("author.birthDate");

Which is equivalent to:

$authorBirthDate = $book->getAuthor()->getBirthDate();

While it doesn't seem that advantageous from just looking at the code, it becomes much more powerful when coupled with PHOCOA bindings.

Let's say you want to have a web page that shows the details of a book. Normally, you'd have to assign the $book object to your template engine and then put something like {$book->getAuthor()->getBirthDate()} in your template code.

With PHOCOA, instead of assigning the $book object to your template, you instead make the $book object one of the instance variables of your module. Then, you set up a WFLabel object and bind the "value" of the WFLabel to the $book variable, and set a keyPath of "author.birthDate" as the modelKeyPath.

Now, even this doesn't seem that different. However, as you might be starting to notice, the keyPath for the WFLabel object isn't programming, it's configuration. So, if you want to change the value of the label, you just edit the configuration of the keyPath and the new value will be used.

Furthermore, because you're using PHOCOA GUI widgets, you get access to a host of additional functionality without having to add any code. Want to truncate the string after 30 characters? Just configure the "ellipsisAfterChars" property of the label. Want to hide the label in certain circumstances? Just bind the "hidden" property of the WFLabel to a function returning a boolean value and PHOCOA does the rest. Want the birthdate to show up as a properly formatted date? Just provide a formatter to the WFLabel. Want to use a formatString to combine multiple values? Use the ValuePattern binding! This is all done via the configuration file, not via coding.