The reason why I chose to use the get_*() and set_*() methods inside the class even though the properties are accessible as well is because if one later wanted to change how to retrieve or set the data only those functions would have to be updated and not everything using them.
RSSFeed::add_item() adds items to the RSS feed. It checks that the data is okay and throws some exceptions if it isn't. It also converts a possible pubDate from a UNIX timestamp to a formatted date. It returns $this to enable the usage of chaining - something which we've used quite a lot in this tutorial actually.
The switch in RSSFeed::parse_item() is there to make the contents of "content" CDATA. Any parser worth using will not parse CDATA and therefore it is not neccessary to convert special characters to HTML entities.
Now we've generated an RSS feed without writing a single line of XML. It is guaranteed to be 100% well-formed and it's easy to add a new item to the feed. In a real application you would get the items from some sort of database and add them to the feed.
Conclusion
Hopefully you've learned a lot about XML and PHP during this tutorial. We've covered the following things:
SimpleXML
PHP DOM
If you have any suggestions feel free to comment here or PM me at the forums.
,
战歌网
数据统计中!!