Task Eleven - Classes - PHP Tutorial
This program introduces the idea of a class. This is a way
to group data (like a card in a card index system holding several data items).
A card in a card index only stores data. A class also has the ability to manipulate
its own stored data. Think of the index card taking on a life of its own.
It is possible to create classes to manage complex data and large amounts of
data.
The example below is simple. It stores a person's first and last name. There
are functions to set the names and retrieve them later.
Here is a screen shot of the program.

Here is the program code.
<?php
// -----------------------------------------------------------------
// ----- A class is a collection of data and functions used to act
// ----- on the data. It is good style to use functions to set
// ----- and get class data and perform other actions within the
// ----- class. It is bad style to act directly on class data from
// ----- outside the class. The code below defines the class but
// ----- does not create a useable object.
// -----------------------------------------------------------------
class Person
{
// CLASS DATA IS SPECIFIED HERE
var $firstName;
var $lastName;
// OUTPUT FROM THE CLASS
function displayName()
{
return "$this->firstName $this->lastName";
}
// INPUT - SET THE FIRST NAME
function setFirstName($newFirstName)
{
$this->firstName = trim($newFirstName);
}
// INPUT - SET THE LAST NAME
function setLastName($newLastName)
{
$this->lastName = trim($newLastName);
}
}
$person = new Person(); // Create an instance of the Person class
// PROMPT FOR AND SET THE FIRST NAME
echo "\n\nPlease enter a first name ... ";
$person->setFirstName(fgets(STDIN));
// PROMPT FOR AND SET THE LAST NAME
echo "\n\nPlease enter a last name ... ";
$person->setLastName(fgets(STDIN));
// DISPLAY THE STORED DATA
echo "\n\nYou entered the following information ... ";
echo $person->displayName();
echo "\n\n";
// -----------------------------------------------------------------
?>
|
Code |
Explanation |
var $firstName;
var $lastName; |
These lines create the variables (pigeon holes) where the names are
to be stored. |
//===========================================
function displayName()
{
return "$this->firstName $this->lastName";
}
//===========================================
|
This function returns a text string consisting of $firstName, a space
character and $lastName.
$this has a special meaning. Your code might have references to firstName
in other parts of the program. $this->firstName uniquely belongs inside
the Person class and will not be muddled up with other instances of firstName. |
| //===========================================
function setFirstName($newFirstName)
{
$this->firstName = trim($newFirstName);
}
//===========================================
|
This function sets the value of $this->firstName. You have to use
the function to pass a value in $newFirstName. It is good style to use
functions to set and retrieve class data values. Programs written this
way are more likely to be reliable and can be modified more easily without
unexpected side effects. |
$person = new Person(); |
This line creates an instance of the Person class. It is possible to
create many instances of a class and the class data does not get muddled
up.
PHP automatically cleans up objects created in this way when there are
no longer any references to the object. |
$person->setFirstName(fgets(STDIN)); |
Whatever you type on the keyboard will be stored in the $person object. |
echo $person->displayName(); |
Use the displayName function to retrieve the data from the Person object. |
Learning Tasks
- Extend the Person class to hold a full name and address.
- Write the necessary functions to input the data following the style guidelines
above.
- Modify the displayName function to display the full name and address.
|