Zend_Feed-CustomFeed.xml 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.feed.custom-feed">
  4. <title>Custom Feed and Entry Classes</title>
  5. <para>
  6. Finally, you can extend the <classname>Zend_Feed</classname> classes if you'd like to
  7. provide your own format or niceties like automatic handling of elements that should go into
  8. a custom namespace.
  9. </para>
  10. <para>
  11. Here is an example of a custom Atom entry class that handles its own <code>myns:</code>
  12. namespace entries. Note that it also makes the <code>registerNamespace()</code> call for
  13. you, so the end user doesn't need to worry about namespaces at all.
  14. </para>
  15. <example id="zend.feed.custom-feed.example.extending">
  16. <title>Extending the Atom Entry Class with Custom Namespaces</title>
  17. <programlisting language="php"><![CDATA[
  18. /**
  19. * The custom entry class automatically knows the feed URI (optional) and
  20. * can automatically add extra namespaces.
  21. */
  22. class MyEntry extends Zend_Feed_Entry_Atom
  23. {
  24. public function __construct($uri = 'http://www.example.com/myfeed/',
  25. $xml = null)
  26. {
  27. parent::__construct($uri, $xml);
  28. Zend_Feed::registerNamespace('myns',
  29. 'http://www.example.com/myns/1.0');
  30. }
  31. public function __get($var)
  32. {
  33. switch ($var) {
  34. case 'myUpdated':
  35. // Translate myUpdated to myns:updated.
  36. return parent::__get('myns:updated');
  37. default:
  38. return parent::__get($var);
  39. }
  40. }
  41. public function __set($var, $value)
  42. {
  43. switch ($var) {
  44. case 'myUpdated':
  45. // Translate myUpdated to myns:updated.
  46. parent::__set('myns:updated', $value);
  47. break;
  48. default:
  49. parent::__set($var, $value);
  50. }
  51. }
  52. public function __call($var, $unused)
  53. {
  54. switch ($var) {
  55. case 'myUpdated':
  56. // Translate myUpdated to myns:updated.
  57. return parent::__call('myns:updated', $unused);
  58. default:
  59. return parent::__call($var, $unused);
  60. }
  61. }
  62. }
  63. ]]></programlisting>
  64. <para>
  65. Then to use this class, you'd just instantiate it directly and set the
  66. <code>myUpdated</code> property:
  67. </para>
  68. <programlisting language="php"><![CDATA[
  69. $entry = new MyEntry();
  70. $entry->myUpdated = '2005-04-19T15:30';
  71. // method-style call is handled by __call function
  72. $entry->myUpdated();
  73. // property-style call is handled by __get function
  74. $entry->myUpdated;
  75. ]]></programlisting>
  76. </example>
  77. </sect1>
  78. <!--
  79. vim:se ts=4 sw=4 et:
  80. -->