Zend_Feed-CustomFeed.xml 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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
  12. <emphasis>myns:</emphasis> namespace entries. Note that it also makes the
  13. <methodname>registerNamespace()</methodname> call for you, so the end user doesn't need to
  14. worry about namespaces at all.
  15. </para>
  16. <example id="zend.feed.custom-feed.example.extending">
  17. <title>Extending the Atom Entry Class with Custom Namespaces</title>
  18. <programlisting language="php"><![CDATA[
  19. /**
  20. * The custom entry class automatically knows the feed URI (optional) and
  21. * can automatically add extra namespaces.
  22. */
  23. class MyEntry extends Zend_Feed_Entry_Atom
  24. {
  25. public function __construct($uri = 'http://www.example.com/myfeed/',
  26. $xml = null)
  27. {
  28. parent::__construct($uri, $xml);
  29. Zend_Feed::registerNamespace('myns',
  30. 'http://www.example.com/myns/1.0');
  31. }
  32. public function __get($var)
  33. {
  34. switch ($var) {
  35. case 'myUpdated':
  36. // Translate myUpdated to myns:updated.
  37. return parent::__get('myns:updated');
  38. default:
  39. return parent::__get($var);
  40. }
  41. }
  42. public function __set($var, $value)
  43. {
  44. switch ($var) {
  45. case 'myUpdated':
  46. // Translate myUpdated to myns:updated.
  47. parent::__set('myns:updated', $value);
  48. break;
  49. default:
  50. parent::__set($var, $value);
  51. }
  52. }
  53. public function __call($var, $unused)
  54. {
  55. switch ($var) {
  56. case 'myUpdated':
  57. // Translate myUpdated to myns:updated.
  58. return parent::__call('myns:updated', $unused);
  59. default:
  60. return parent::__call($var, $unused);
  61. }
  62. }
  63. }
  64. ]]></programlisting>
  65. <para>
  66. Then to use this class, you'd just instantiate it directly and set the
  67. <property>myUpdated</property> property:
  68. </para>
  69. <programlisting language="php"><![CDATA[
  70. $entry = new MyEntry();
  71. $entry->myUpdated = '2005-04-19T15:30';
  72. // method-style call is handled by __call function
  73. $entry->myUpdated();
  74. // property-style call is handled by __get function
  75. $entry->myUpdated;
  76. ]]></programlisting>
  77. </example>
  78. </sect1>
  79. <!--
  80. vim:se ts=4 sw=4 et:
  81. -->