Professional Documents
Culture Documents
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
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()
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();
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;
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;
}
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
$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
Summary
Now that you have completed this unit, you should be able to
Problems to solve
1. Write a PHP program that accepts the registration details of a Student as follows
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