Article

eZ publish: PHP's Killer App - Parts 1-3

Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 Next

Next we have the ACCESSORS, which we use to access member variables (that might have been set by the get() method, for example):

   // ACCESSORS                  
   /*!                  
    Returns the current faq id, for links etc.                  
   */                  
   function id() {                  
      return $this->ID;                  
   }                  
                 
   /*!                  
    Returns the current faq question                  
   */                  
   function question() {                  
       return $this->Question;                  
   }                  
                 
   /*!                  
    Returns the current answer                  
   */                  
   function answer () {                  
       return $this->Answer;                  
   }                  
                 
   /*!                  
    Returns the date                  
   */                  
   function date () {                  
       $locale= new ezLocale;                  
       $datetime = new ezDate;                  
       $datetime->setTimeStamp($this->Date);                  
       return $locale->format($datetime);                  
   }                  
                 
   /*!                  
    Returns the Published switch                  
   */                  
   function published () {                  
       return $this->Published;                  
   }                  
                 
   /*!                  
    Returns the current heading id                  
   */                  
   function headingId () {                  
       return $this->HeadingID;                  
   }

One thing to notice is the date() method, which takes advantage of the ezLocale and ezDate classes to convert a UNIX time stamp into a "user friendly" date. In case you weren't aware, a UNIX time stamp is the number of seconds that have passed since the beginning of 1970 (it could have been 1969 but no one's too sure how many seconds passed that year). For an in-depth discussion of dates and times in PHP, head to Date/Time Processing With PHP.

Finally we have the data members:

   // DATA MEMBERS                  
   var $ID;                  
   var $Question;                  
   var $Answer;                  
   var $Date;                  
   var $Published;                  
   var $HeadingID;                  
}

For those of you used to putting the data members at the start of a class, I'm following the eZ publish convention. There's nothing to stop you placing the var definitions at the end of the class in PHP, and in terms of the way people think, perhaps it's more logical to start with the class member functions, then follow with the member variables, to help make the code readable.

So that's our parent eZFaq class complete! Notice that it makes no comment on what the database is (it uses the eZDB abstract class) and it doesn't output anything directly to the end user (do you see any XHTML anywhere?). We can do anything we like with the variables it returns to us -- put them in an XHTML page, convert them into an XML document, place them in a flash movie -- whatever takes our fancy!

Moving on again, let's look at the child eZFaqAdmin class.

class eZFaqAdmin extends eZFaq {                  
   // CREATORS                  
   /*!                  
     Constructs a new eZFaqAdmin object.                  
                 
     If $id is set the object's values are                  
     fetched from the database.                  
   */                  
   function eZFaqAdmin ( $id=-1 ) {                  
       eZFaq::eZFaq ( $id );                  
   }

It starts of by extending the parent, and is capable for receiving FAQ IDs as well, which it hands off to the parent constructor using eZFaq::eZFaq ( $id );.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links