Home > Articles > Programming > PHP

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

Create a Class

So far, we've modularized our code somewhat, but we haven't really made it any easier to read—much less use. Now we're ready to start creating the actual objects, which should make our lives a bit easier.

Before we can create an object, we need to create a class. The class will act as a template for the object, defining its properties and methods. Any number of objects can be created from a single class, and they will be independent of each other. Each is considered an "instance" of the class.

For example, a model of car might be considered a class because it defines the basic information about the car—such as what parts it has and how they fit together, and what happens when you push a particular button on the dashboard. Until the company actually manufactures one of these cars, however, the model is just an idea. Each actual car can be considered an object and an instance of the model class.

In our case, we will create a class for events. Ultimately, the Event class will define the way in which events are added to or edited within the database, and will provide an easy way to get information about a particular Event object.

For now, however, we'll create a simple class with arbitrary information, as shown in Listing 3, so you can see how classes are created and used.

Listing 3—Creating a Simple Class in showevent.php

<?php
class Event {

  function Event($this_eventid){

    $this->eventid = $this_eventid;
    $this->month = "Month";
    $this->day = "Day";
    $this->year = "Year";
    $this->title = "Event Title";
    $this->description = "Event Description";

  }

}

  import_request_variables('pgc', '');

  $this_event = new Event($eventid);

  printf ( "<h2>%s</h2>", $this_event->title );
  printf ( "<h3>Date: %s/%s/%s</h3>", 
         $this_event->month, $this_event->day, $this_event->year );
  printf ( "<p>%s</p>", $this_event->description );
  printf ( "<a href='saveevent.php?editid=%s'>Edit this event</a>", $this_event->eventid );

?>

Let's start at the top. First, we're creating a new class (Event), which has a single method that is also called Event(). Because it shares its name with the class, this method is called a constructor. A constructor is executed when an object is created, or instantiated. For example, when the program executes the following line, PHP creates the new object and causes it to execute the Event() method:

$this_event = new Event($eventid);

The method itself is pretty simple: setting six object properties with arbitrary values. In object-oriented parlance, the $this keyword always refers to the object itself, so when we execute the following statement, we're setting the month property of the current object to Month. (In Java, we might write this as this.month = "Month";.)

$this->month = "Month";

After we create the object itself, we can access these properties using the same notation. Because $this_event represents the newly created object, we can simply retrieve the following to get the value of the month property:

$this_event->month

Note that in most circles it's considered bad form to directly access the value of a property like this; it's preferable to use methods, which we'll cover later in the article.

  • + Share This
  • 🔖 Save To Your Account