2
0

Zend_View-Abstract.xml 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.view.abstract">
  4. <title>Zend_View_Abstract</title>
  5. <para>
  6. <classname>Zend_View_Abstract</classname> is the base class on which
  7. <classname>Zend_View</classname> is built; <classname>Zend_View</classname> itself simply
  8. extends it and declares a concrete implementation of the
  9. <code>_run()</code> method (which is invoked by <code>render()</code>).
  10. </para>
  11. <para>
  12. Many developers find that they want to extend
  13. <classname>Zend_View_Abstract</classname> to add custom functionality, and
  14. inevitably run into issues with its design, which includes a number of
  15. private members. This document aims to explain the decision behind the
  16. design.
  17. </para>
  18. <para>
  19. <classname>Zend_View</classname> is something of an anti-templating engine in that
  20. it uses PHP natively for its templating. As a result, all of PHP is
  21. available, and view scripts inherit the scope of their calling object.
  22. </para>
  23. <para>
  24. It is this latter point that is salient to the design decisions.
  25. Internally, <classname>Zend_View::_run()</classname> does the following:
  26. </para>
  27. <programlisting language="php"><![CDATA[
  28. protected function _run()
  29. {
  30. include func_get_arg(0);
  31. }
  32. ]]></programlisting>
  33. <para>
  34. As such, the view scripts have access to the current object
  35. (<code>$this</code>), <emphasis>and any methods or members of that
  36. object</emphasis>. Since many operations depend on members with
  37. limited visibility, this poses a problem: the view scripts could
  38. potentially make calls to such methods or modify critical properties
  39. directly. Imagine a script overwriting <code>$_path</code> or
  40. <code>$_file</code> inadvertently -- any further calls to
  41. <code>render()</code> or view helpers would break!
  42. </para>
  43. <para>
  44. Fortunately, PHP 5 has an answer to this with its visibility
  45. declarations: private members are not accessible by objects extending a
  46. given class. This led to the current design: since
  47. <classname>Zend_View</classname> <emphasis>extends</emphasis>
  48. <classname>Zend_View_Abstract</classname>, view scripts are thus limited to only
  49. protected or public methods and members of
  50. <classname>Zend_View_Abstract</classname> -- effectively limiting the actions it
  51. can perform, and allowing us to secure critical areas from abuse by view
  52. scripts.
  53. </para>
  54. </sect1>
  55. <!--
  56. vim:se ts=4 sw=4 et:
  57. -->