PHP5 Class Structure

PHP5 Class Structure

Classes are a very important aspect of good code. Organizing your code in classes makes the code reusable (even in other applications) and helps clearly separate the model from the view of the site.

Components of a Class

Classes contain attributes and methods. The attributes are variables that describe the object and methods are functions that manipulate the object to accomplish various tasks.

For example, we could have a class for a Car. A car has a color, a maximum speed, a certain amount of seats, and tires. Each of those things could be an attribute for a Car class.

A Car can have its engine started and stopped, it can accelerate and slow down. These things could be represented as methods.

A Class in PHP5

To create a Car class in PHP5 you would enter the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Car {
  private $color;
  private $max_speed;
  private $num_seats;
  private $tires;
 
  public function __construct() {
    $this->color = ‘Red’;
    $this->max_speed = 150;
    $this->num_seats = 4;
    $this->tires = ‘Radial’
  }
  public function accelerate() {
    // Accelerate The Car
  }
  public function brake() {
    // Push The Brake
  }
  public function start() {
    // Start The Engine
  }
  public function stop() {
    // Stop The Engine
  }
}

On line 1 we declare the class. In this example we have called the class Car, however we could have easily called it something else like Automobile.

On lines 2-5 we declare the variables that are the attributes for the Car. You will notice that all the variables have the word private before them. This keeps the variables from being accessed of set from outside the object. You should get in the habit of setting your variables to private and creating functions to get and set them. While this seems like a lot of work it allows you to protect the data within your object. For example imagine we had a variable called engine_started. Suppose in our Class we were going down the road at 50 MPH. Now imagine we just set engine_started to false. It would stop the engine but keep us moving at 50 MPH. Not the best situation to be in. You want to be able to validate your information as it changes.

Line 6-11 declare the object constructor. The constructor is run when the object is created. In lines 7-10 you see that we are setting the attribute variables for the class. This is how you set and get items from within the class. $this represents the object that you are currently in.

Using The Class

Using a class in PHP is very simple. The first thing you have to do is create an instance of the object. That is done in the following syntax:

$car = new Car();

This creates a new instance of the Car class. It runs the constructor and sets all the variables.

To run the various methods in the class you use the following syntax:

$car->accelerate();

$car->brake();

$car->start();

$car->stop() ;

One thing you cannot do is get the Car’s instance variables. You will have to create functions to get and set the values in order to do this.

If you are a regular reader of the site you will notice that this post replaced a post about objects and classes.  I hope that this will be an improvement over the last iteration.  I will continue to update this post as I can in order to improve the information.

If you have any questions post a comment and I will do my best to get back to you as soon as possible.

Leave a Reply