Which is better way to pass data to views?

In the views the current controller/widget is accessible with $this.

Which one is better way to pass data to the views and why:

  • to pass every data needed as array in the render method
  • to make data property of the controller/widget and use $this->property in the view

Thanks in advance!

I think the first solution is better, because if you create properties in the controller/widget, they will be globally (that is, wherever the controller/widget is available) available and that might not always be a good thing.

I always create arrays with keys => data and pass the array in the render() view.

Yes and that way the only contract between the view and its controller is actually the array passed… The two of them are loosely coupled - every body is happy… One thing that bothers me is that there is no type-checking available… I mean - I want some of my views to be called only and onle by exact type of controller/widget…

Also in some cases you may want to pass almost all (or just many) fields, and in some cases there can be some kind of render-time calculation that must be done by the controller… Then I guess I must use $this.

But as far as I know - if there is "worse case scenario" in OOP design, the design should be built against it… So that is my dilema now…