SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    May 2007
    Location
    West Midlands, UK
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Inheritance Issue

    I'm parsing RSS feeds. I have created a parent class, which has the common variables, and functions, then each child class will have its own function to parse the feed in its own way depending on what feed is being parsed.

    Here is the parent rss.php class"

    PHP Code:
    class rss {

        protected 
    $feed;
        protected 
    $rssItems = array();
        
        public function 
    __construct($rssFeed) {
            try {
                
    $this->feed = new SimpleXMLElement($rssFeedNULLTRUE);
            } catch(
    Exception $e) {
                print 
    'There was an error with the feed.';
            }
        }
        
        public function 
    countRSSFeedItems() {
            return 
    count($this->rssItems);
        }

        public function 
    getRSSFeedItems() {
            return 
    $this->rssItems;
        }
        
        

    Here is the rss10.php class

    PHP Code:
    class rss10 extends rss {

        public function 
    __construct($rssFeed) {
            
    parent::__construct($rssFeed);
            
    $this->extractRSSFeedItems();
        }
        
        private function 
    extractRSSFeedItems() {
            foreach(
    $this->feed->item as $item) {
                
    $this->rssItems[] = new rssItem($item->title$item->link$item->description);
            }
        }
        

    Now, each class will have the same constructor, so I have included it in the parent class BUT this is the problem. I want to put:

    PHP Code:
    $this->extractRSSFeedItems(); 
    In the parent class, but because the method does not exist in the parent class, will it get called when creating the new child class? Or would I need to keep it in each child class constructor?

    Only other thing I thought off was to create a default blank method in the parent.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could call extractRSSFeedItems() in the superclass constructor. But, you should declare rss as an abstract class, and declare extractRSSFeedItems() as an abstract method. This way, you're guaranteed that an implemented version of the method will exist so long as the object is of type rss, because you won't be able to instantiate rss(the class is abstract, but a class must be concrete to be instantiated). The method will need to be protected, not private.

    I'm curious what peoples opinion on this are though. extractRSSFeedItems() isn't a public method, and it's just an implementation specific method. Seems like your forcing the subclass to contruct itself in a certain manor, when it might be better to let it worry about how it wants to do it.

  3. #3
    SitePoint Zealot
    Join Date
    May 2007
    Location
    West Midlands, UK
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    You could call extractRSSFeedItems() in the superclass constructor. But, you should declare rss as an abstract class, and declare extractRSSFeedItems() as an abstract method. This way, you're guaranteed that an implemented version of the method will exist so long as the object is of type rss, because you won't be able to instantiate rss(the class is abstract, but a class must be concrete to be instantiated). The method will need to be protected, not private.

    I'm curious what peoples opinion on this are though. extractRSSFeedItems() isn't a public method, and it's just an implementation specific method. Seems like your forcing the subclass to contruct itself in a certain manor, when it might be better to let it worry about how it wants to do it.
    Well all subclasses will construct themselves in exactly the same way, so I would rather the parent class take care of that.

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    Would it be out of the question to make rss a abstract class with a abstract method extractRSSFeedItems()?

    I don't think there is anything wrong with a empty method to keep a consistent interface compatible.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •