advertisement

Learn a quick introduction to OOP (Object Oriented Programming). We will be creating a small class for fun to demonstrate how it all works.

Today you will be creating a small class as an introduction to OOP which stands for Object Oriented Programming. Classes in PHP are very powerful and allow you to do a lot of great things, for the sake of starting off slow though today we will be creating a basic example of a class just to get your feet wet.

The example shown here in this tutorial will work on either PHP 4.X or 5.X, but a lot of great Class functions are only available in PHP 5. In this tutorial we will be creating a class that will allow us to add up cars and then display the total weight of all the cars and also change the default weight per car.

So let’s start the actual code part, for our script we will have 2 files. The first file we will be creating will contain our class, then the second file will include our class file and use our class to display the information we want.

<?php
class CarCounter
{
var $cars = 0;
var $weightPerCar = 500.0;

function add($n = 1)
{
$this->cars = $this->cars + $n;
}

function totalWeight()
{
return $this->cars * $this->weightPerCar;
}
}
?>

I just gave you the total code for our car class, I will now break it down and explain it. First we must define our new class by simply putting class then the name of the class that you want to create.

Then we define a variable. We create a variable called $cars. We will be using this variable to define how many cars we actually have, so we set this to 0 because we dont have any actual cars at the moment. Next we define another variable called $weightPerCar. This is the default weight per car. I have set this to 500.0, just a fictional number. Later on we will be changing the default weight though. Now we define a function within our class, this function will be used to just add more cars to our total. So we rightfully call this function cars. Then all we are doing is giving our function cars an argument, this is where we let the user put in as many cars as he wants to be added, we use the variable $n which will be used later on in the same function, we set $n to a default of 1. Then all we do is add $n to the current total.

Now we define our totalWeight function. This is where we calculate the total weight of all our cars by using the default totalWeight that we defined earlier. This works the same way as our add function except we are multiplying cars by totalWeight to get the weight of ALL our cars.

Then we close off our function and class and we are done with class.php, now we move onto cars.php where we actually use our class.

<?php
require "class.php";

$cars = new CarCounter;
$cars->cars = 5;

print "There are " . $cars->cars . " cars<br />";

$cars->add(10);

print "There are " . $cars->cars . " cars<br />";

print "Total weight = " . $cars->totalWeight() . " kg<br />";

$cars->weightPerCar = 700.0;

print "With the new weight per car, your total weight is now " . $cars->totalWeight() . " kg";
?>

This is where we actually use our class, now to use our class we must first include it in our file. That is where we use the require. Now that our class file is included we can now use the class we defined in that file. Now to use the class and the member variables defined in the class we must create a new instance of the class. We can define our instance to a variable to use in our script.

This is where we have -

$cars = new CarCounter;

Once we have defined a new instance of our class we can now use the member variables defined in our class. So then we add 5 cars to our total.

Then we print what we have so far. Using our variable $cars, we can then access which member variable we need. cars was our member variable in our class that was set to 0. So now we set it to 5 to give it a value.

Now let’s say we want to add more cars to our total we use our add function we created. So we use it the same way we would use a regular function except we must first say that it is in our class (Which is defined to our variable of $cars). So we add 10 more cars to our total so we now have 15 cars, and to prove this we print it out the exact same way we printed it out the first time.

Now with our 15 cars we want to find out the total weight of all 15 of these cars using the default weight we defined as one of our member variables. So we just do another print statement except we use the totalWeight function.

Now let’s say we wanted to change the default weight of our cars, we can just give it a new value.

$cars->weightPerCar = 700.0;

Then to prove that the weight actually did change we print out our totalWeight again except this time the function is using a new totalWeight default value so our total will be higher.

Thanks for reading this tutorial, if you have any questions use the contact form and we will address your comments or questions!

Thanks
Sean

advertisement