View Objects: Template Patterns in Plain PHP

← All talks

Abstract

What if a view’s required inputs were declared in a constructor, and rendering it was simply a method call?

This session explores view objects: ordinary PHP classes that receive typed data and render HTML when invoked. Used as an optional rendering approach in Unphramed, an explicit MVC architecture for PHP, these patterns can also fit applications built with other architectures or frameworks.

Starting with a small example, we will build towards reusable components and page layouts. We will translate familiar template concepts—includes, inheritance, blocks and macros—into object composition, method overriding and typed helpers, examining where those mappings work and where composition offers a better fit.

Examples drawn from a real application will expose the trade-offs: explicit contracts and refactoring support, inheritance versus composition, and responsibility for context-appropriate escaping.

You will leave with practical patterns for building PHP view objects and criteria for deciding when a dedicated template engine remains the better choice. No prior knowledge of Unphramed or attendance at its architecture session is required.