Article
eZ publish: PHP's Killer App - Parts 1-3
Sketching a Class
A good trick help you start to write a PHP class is to begin by just laying it out as a whole, while avoiding writing code for particular member functions. Here's my sketch of the eZFaq class:
<?php
//!! eZFaq
//! Base class for public use
class eZFaq {
// CREATORS
/*!
Constructs a new eZFaq object.
*/
function eZFaq( ) {
}
// MANIPULATORS
/*!
Gets a single faq from the database
*/
function get( ) {
}
/*!
Gets a list of FAQs from the database
*/
function &getAll( ) {
}
/*!
Searches the ezfaq_faq table for a string
*/
function &search() {
}
// ACCESSORS
/*!
Returns the current faq id, for links etc.
*/
function id() {
}
/*!
Returns the current faq question
*/
function question() {
}
/*!
Returns the current answer
*/
function answer () {
}
/*!
Returns the date
*/
function date () {
}
/*!
Returns the Published switch
*/
function published () {
}
/*!
Returns the current heading id
*/
function headingId () {
}
}
Easy wasn't it?
Let's see what we have here. First we have "CREATORS": methods that create something when the eZFaq object is instantiated. Then we have "MANIPULATORS": member functions that collect data from somewhere and transform it. These correspond to the events we want our site visitors to be able to perform. And finally we have "ACCESSORS": functions we can use to access the objects member variables (notice that these correspond to the fields in the database).
One thing to pay attention to is: function &getAll() {. This will return a reference to some data, rather than the data itself. For more detail on references, try this article: PHP References explained.
You may also been wondering about the exclamation marks that appear in some of the comments. The eZ publish class documentation is generated automatically from the code and comments using eZ phpdoc -- a nice way to save time writing documentation...
OK -- that deals with all the actions we want to allow site visitors to perform. What about the phpPoint administrators?
class eZFaqAdmin extends eZFaq {
// CREATORS
/*!
Constructs a new eZFaqAdmin object.
Calls the parent constructor
*/
function eZFaqAdmin ( ) {
eZFaq::eZFaq ( );
}
// MANIPULATORS
/*!
Inserts or updates a faq
*/
function store() {
}
/*!
Deletes a faq
*/
function delete( ) {
}
// ACCESSORS
/*!
Assigns faq question to local member variable
for store() method
*/
function setQuestion() {
}
/*!
Assigns faq answer to local member variable
for store() method
*/
function setAnswer($Answer) {
}
/*!
Assigns the Published switch for the store() method
*/
function setPublished ($Published) {
}
/*!
Sets the current heading id
*/
function setHeadingID ($HeadingID) {
}
}
?>
Now we've covered all the administrator "events" as well, providing the method store() to edit and create FAQs, while delete covers the deletion of FAQs. The ACCESSORS we'll be using here will give data to the object. What's more, as eZFaqAdmin extends eZFaq: if we instantiate eZFaqAdmin, we can access to everything eZFaq has to offer as well.
Note: in the CREATOR method eZFaqAdmin(), we find eZFaq::eZFaq ( );. Here we've use something called the paamayim nekudotayim operator -- ::. This is used to invoke a member function of a class without instantiating the entire class. In this case it's important -- when you instantiate a child class, only its own constructor will be invoked, not the parent constructor. But for our class, we want the parent constructor to be invoked (for reasons we'll see in a moment), so we specifically tell the child to do so.
And that's it. Our eZFaq classes our finished! Well... almost: we just need to fill in the blanks.