Zend_View-Abstract.xml 2.7 KB

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