Zend_Feed-CustomFeed.xml 2.7 KB

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