Custom Feed and Entry Classes

Finally, you can extend the Zend_Feed classes if you'd like to provide your own format or niceties like automatic handling of elements that should go into a custom namespace.

Here is an example of a custom Atom entry class that handles its own myns: namespace entries. Note that it also makes the registerNamespace() call for you, so the end user doesn't need to worry about namespaces at all.

Example #1 Extending the Atom Entry Class with Custom Namespaces

  1. /**
  2. * The custom entry class automatically knows the feed URI (optional) and
  3. * can automatically add extra namespaces.
  4. */
  5. class MyEntry extends Zend_Feed_Entry_Atom
  6. {
  7.  
  8.     public function __construct($uri = 'http://www.example.com/myfeed/',
  9.                                 $xml = null)
  10.     {
  11.         parent::__construct($uri, $xml);
  12.  
  13.         Zend_Feed::registerNamespace('myns',
  14.                                      'http://www.example.com/myns/1.0');
  15.     }
  16.  
  17.     public function __get($var)
  18.     {
  19.         switch ($var) {
  20.             case 'myUpdated':
  21.                 // Translate myUpdated to myns:updated.
  22.                 return parent::__get('myns:updated');
  23.  
  24.             default:
  25.                 return parent::__get($var);
  26.             }
  27.     }
  28.  
  29.     public function __set($var, $value)
  30.     {
  31.         switch ($var) {
  32.             case 'myUpdated':
  33.                 // Translate myUpdated to myns:updated.
  34.                 parent::__set('myns:updated', $value);
  35.                 break;
  36.  
  37.             default:
  38.                 parent::__set($var, $value);
  39.         }
  40.     }
  41.  
  42.     public function __call($var, $unused)
  43.     {
  44.         switch ($var) {
  45.             case 'myUpdated':
  46.                 // Translate myUpdated to myns:updated.
  47.                 return parent::__call('myns:updated', $unused);
  48.  
  49.             default:
  50.                 return parent::__call($var, $unused);
  51.         }
  52.     }
  53. }

Then to use this class, you'd just instantiate it directly and set the myUpdated property:

  1. $entry = new MyEntry();
  2. $entry->myUpdated = '2005-04-19T15:30';
  3.  
  4. // method-style call is handled by __call function
  5. $entry->myUpdated();
  6. // property-style call is handled by __get function
  7. $entry->myUpdated;
blog comments powered by Disqus