Home > Articles > Programming > PHP

  • Print
  • + Share This
Like this article? We recommend

Using Object Methods

Now that we have the class set up and separated into its own file, we can call it from anywhere on the site. For example, we can use it to simplify the processing of the saveevent.php page, which displays a form that takes event information and saves it to the database.

Listing 8 shows the conversion of saveevent.php to an object-oriented page.

Listing 8—Using Objects to Create and Edit Events



import_request_variables('p', 'p_');
import_request_variables('g', 'g_');

//If no description provided, display form
if (!isset($p_description)) {

//If an id is provided, this is an edit page rather than an add page

  if (isset($g_editid)){
    $this_event = new Event($g_editid);
    $d_month = $this_event->get_month();
    $d_day = $this_event->get_day();
    $d_year = $this_event->get_year();
    $d_title = $this_event->get_title();
    $d_description = $this_event->get_description();
  } else {
    $d_month = $g_month;
    $d_day = $g_day;
    $d_year = $g_year;
  <form action="saveevent.php" method="post">
    <input type="hidden" name="oldid" value="<?= $g_editid ?>" />
    Month: <input type="text" name="month" value="<?= $d_month ?>" /><br />
    Day: <input type="text" name="day" value="<?= $d_day ?>" /><br />
    Year: <input type="text" name="year" value="<?= $d_year ?>" /><br />
    Title: <input type="text" name="title" value="<?= $d_title ?>" /><br />
    Description <textarea name="description" cols="60" rows="4">
    <?= $d_description ?></textarea><br />
    <input type="submit" value="Save Event" />

} else {

  if ($p_oldid != ""){
    echo ("Editing record...");
    $this_event = new Event($p_oldid);
    $this_event->save_event($p_oldid, $p_month, $p_day,
                            $p_year, $p_title, $p_description);
  } else {
    echo ("Adding record...");
    $this_event = new Event(-1);
    $this_event->create_event($p_month, $p_day,
                              $p_year, $p_title, $p_description);


Let's take this page one step at a time. First, we include the class definition using the require() function, and including any submitted information using the import_request_variables() function. If we're pulling up the form itself, information will be submitted via GET as part of a URL, but if we're processing the submission of the form, we're looking for information submitted using POST, so we need to import both sets of values.

An event must have a description, so if one is not included in the POSTed information, we will display the form. If we're trying to edit an event, we'll have a value for $g_edited. So if we find one, we'll simply create an Event object, $this_event, with the appropriate eventid; and then retrieve its properties using the getter methods and store them in variables. If no eventid was submitted, we're creating a new event, so we'll simply set the page variables (those that begin with $d_) to the date values that were submitted as part of the link. (Notice the $g_ values.)

After we set the values, we can create the form itself. The form takes the $d_ variables and uses them as default information on the form, so the form will work whether we're working with an existing event or not. If we are working with an existing event, we'll save the eventid as oldid so we know what event to edit later.

Now, all of that presupposes that no description was POSTed. If there was a description included in the POST information, we know we need to save the event. If there's a value for $p_oldid, we know we're editing an event, so we'll use that eventid to create a new Event object. We can then execute the save_event() method, feeding it all of the new information.

If there's no value for $p_oldid, we're dealing with a new event; so we'll create an Event object using [ms]1 as the eventid. All this does is make sure that the constructor doesn't find an existing event, so none of the properties will be populated. We can then execute the create_event() method to actually save the new event to the database.

Notice that the actual mechanics are not included in the page, so we can change the way information is stored without affecting the main application. For example, it's probably better practice if within the create_event() and save_event() methods we used the setter methods to actually populate the object; and then used the getter methods to create the SQL statements that are executed. Because we're simply accessing the properties within the setter and getter methods, the results would be the same in this case. If that weren't the case, however, we could fix the problem without breaking the main application.

  • + Share This
  • 🔖 Save To Your Account