You are on page 1of 11

Course Handout Student Notebook

Contents
Unit 6: Class and Object 2
Learning Objectives 2
Introduction 3
Object 3
Class 3
Defining Class in PHP 3
Object in PHP 4
Usage of $this variable 5
Constructor 5
Constructor with Parameters 6
Summary 8
Problems to solve 9

Copyright IBM Corp. 2011 Course Contents 1


Course Materials may not be reproduced in w hole or in part
w ithout the prior permission of IBM.
PHP Core Student Notebook

Unit 6: Class and Object


Learning Objectives

At the end of this unit, you will be able to:

Understand Object Oriented Language and its role in the system


Understand the concept of Class and Object
Create a class and an Object
Understand the concept of constructors and its usage

Copyright IBM Corp. 2011 Unit 6 PHP Basics 2


Course Materials may not be reproduced in w hole or in part
w ithout the prior permission of IBM.
Student Notebook PHP Core

Introduction
Support for Object Oriented Language was introduced from PHP 3. There was no major improvement in the
features of PHP 4 as backward compatibility was the main concern. When demand for the OOP approach
increased, many new features were introduced and added in PHP 5

Object
An Object is a real world entity of the problem domain under concern, which is described through certain
property and has a definite role to play in the system. Object Oriented Applications have several such objects
that interact with each other to perform a business operation.
For example, An Online Shopping Cart application would have Customer and Product objects where Customer
purchases a Product online.

Class
A class is a template that describes attributes and methods of a certain object type.
For example, the Customer class defines the attributes and behaviors that all Customers have and perform in an
Online Shopping Cart.

Customer

Property:
name
age

Methods:

setName($name)
setAge($age)
getName()
getAge()

Defining Class in PHP


Class in PHP is created using the following Syntax.
class Classname
{ //Define Properties
//Define Methods
}
The Customer class in PHP is defined as follows
class Customer
{
private $name;
private $age;

3 PHP Copyright IBM Corp.2011


PHP Core Student Notebook

function setName($name)
{
$this->name=$name;
}
function setAge($age)
{
$this->age=$age;
}
function getName()
{
return $this->name;
}
function getAge()
{
return $this->age;
}
}

Object in PHP
Object is a runtime instance of a class.
From class Customer, we can create many Customer Objects. Every Object is an instance of Customer

Philip
Joe Philip Sam

Philip

Creating an Object
An Object is created using the new keyword. The following code creates a Customer object
$custobj=new Customer();
To assign the values for object property, the setter method is called.
$custobj->setName("Joe");
$custobj->setAge(25);
To access the property values, the getter method is called.
print "Customer Name:".$custobj->getName();

Copyright IBM Corp. 2011 Unit 6 PHP Basics 4


Course Materials may not be reproduced in w hole or in part
w ithout the prior permission of IBM.
Student Notebook PHP Core

print "<br>";
print "Customer Age:".$custobj->getAge();
The output of the above code is
Customer Name: Joe
Customer Age:25
Private property cannot be accessed directly outside the class. To retrieve and modify its values we need to use
the getter and setter methods. The keyword private is used before the property variable to declare it as private.
Alternatively a property can also be declared public. Public property, as the name indicates can be accessed
outside the class .The keyword public is used before the property variable to declare it as public .
If customer name had been declared public then the values of it can be set and retrieved as follows ;
class Customer
{
public $name;
public $age;

//other code
}
$custobj=new Customer();
$custobj->name=Joe;
print $custobj->name;

Usage of $this variable


The $this variable is automatically defined during the execution of the objects method. It is used to refer the
properties of an Object.

Constructor
Constructors are used to initialize the state of the object during object creation.
Constructor is a function defined using __construct keyword
Constructor is automatically called during object creation, using the new keyword
Constructor can accept parameters

class Customer
{
private $name;
private $age;
function __construct()
{
$this->name="Joe";
$this->age=25;

5 PHP Copyright IBM Corp.2011


PHP Core Student Notebook

}
function setName($name)
{
$this->name=$name;
}
function setAge($age)
{
$this->age=$age;
}
function getName()
{
return $this->name;
}
function getAge()
{
return $this->age;
}
}
The Customer object is created as usual, but this time the constructor is called and the name and age of the
customer is initialized as defined in the constructor.
$custobj=new Customer();
Getter methods are used to retrieve customer details.
print "Customer Name:".$custobj->getName();
print "<br>";
print "Customer Age:".$custobj->getAge();
The output of the above code will be
Customer Name: Joe
Customer Age: 25

Constructor with Parameters


Using the Constructor defined above, all customers will be Joe with age 25. Incidentally few customers can have
the same name and age, but practically not all customers can have the same name and age.
To create a Customer object with meaningful details, a parameterized constructor is used. Instead of creating a
constructor with hard coded values, a constructor that can accept parameters is used.
We can rewrite the constructor defined above as follows;
function __construct($name,$age)
{
$this->name=$name;

Copyright IBM Corp. 2011 Unit 6 PHP Basics 6


Course Materials may not be reproduced in w hole or in part
w ithout the prior permission of IBM.
Student Notebook PHP Core

$this->age=$age;
}
Customer objects are created using the parameterized constructor
$custobj1=new Customer("Philip",45);
$custobj2=new Customer("Joe",25);
The following code snippet prints the customer details
print "Customer Name:".$custobj1->getName();
print "<br>";
print "Customer Age:".$custobj1->getAge();
print "<br>";
print "Customer Name:".$custobj2->getName();
print "<br>";
print "Customer Age:".$custobj2->getAge();
The output of the above code will be
Customer Name:Philip
Customer Age:45
Customer Name:Joe
Customer Age:25

7 PHP Copyright IBM Corp.2011


PHP Core Student Notebook

Summary
Now that you have completed this unit, you should be able to

Understand Object Oriented Language and its role in the system


Understand the concept of Class and Object
Create a class and an Object
Understand the concept of constructors and its usage

Copyright IBM Corp. 2011 Unit 6 PHP Basics 8


Course Materials may not be reproduced in w hole or in part
w ithout the prior permission of IBM.
Student Notebook PHP Core

Problems to solve
1. Write a PHP program that accepts the registration details of a Student as follows

The Registration process of the student has to be done as follows;


The entered registration details have to be populated in the Student object. The course fee and duration in the
Student object has to be set according to the course chosen and then the details has to be retrieved from the
student object and displayed onto the web browser
The course fee and duration is calculated as follows.
Normal course, fee=3000 dollars and duration is 4 months
Fast Track course, fee=6500 dollars and duration is 2 months
When the student registers for the course, the registration details along with the course fee and duration has to
displayed in non-editable format as follows

2. Consider the following scenario


The online examination system allows the users to attend exams online and view the results.
Any candidate who is willing to take an online exam has to register himself with the system and obtain a
candidate id and a password. The candidate has to give his name, age, gender, and address and qualification

9 PHP Copyright IBM Corp.2011


PHP Core Student Notebook

details for registration. The candidate has to then logon into the system with the given candidate id, password
and register himself for a particular exam on a particular date. The candidate will be given a registration number
on successful registration for the exam. The exam details comprise of an exam id, description of the exam,
duration of the exam, no of questions, prerequisite details if any based on which the candidate can register for
the exam. The candidate can change the default password given to him by the system, if required. The
candidate can then attend the exam by giving the registration number. When he/she completes the exam and
clicks finish, the results are displayed giving the percentage of marks and the status of result (pass or fail)
Identify the objects relevant to the domain. Define the class, properties and behaviors accordingly .

3. Create a web application that takes the faculty feedback from the student and stores it in a file. Given the
subject name and the faculty name, the application has to display the average feedback for each criteria and
the comments given by all students.
The student has to enter the feedback form with the following details, in the given format ;

Create a class called as Feedback to hold the details as mentioned above. When the student clicks the Submit
Feedback button, the details has to be stored in the Feedback object after which the details are retrieved and
stored in a file.
When the faculty name and the subject is given, feedback summary is displayed as follows

Copyright IBM Corp. 2011 Unit 6 PHP Basics 10


Course Materials may not be reproduced in w hole or in part
w ithout the prior permission of IBM.
Student Notebook PHP Core

11 PHP Copyright IBM Corp.2011

You might also like