Foreach(Array Expression As $Value) Statement

Foreach(Array Expression As $Value) Statement

Server Side Programming with PHP CPAN 542

Lecture 11: Object Oriented PHP

PHP has basic support for classes and objects. Classes are user-defined data types that are compound of member variables and member functions. Member variables represent properties of the object and the member functions represent actions that can be performed by objects. You can initialize the member variables to constants directly, otherwise you have to use the class constructor for this purpose. An object of a class is an instance of this class that encapsulates the properties and actions of this class.

An Object is a software bundle of related variables and methods. Objects are used to model real life objects and events.

A class is a template that defines the variables and the methods that are common to objects of related kind. Classes describe the attributes and behaviors of objects.

A class is declared using the keyword class.The class attributes are variables declared inside the class declaration to describe the class attributes . The class methods are functions declared inside the class declaration to describe the actions that can be taken by the class . The class constructor is a function with the same name as the class name. The class constructor can have parameters, but has no return value. For example:

class Student
{
var $stName;
function Student($name)
{
$this->$stName=$name;
}
function getInfo()
{
return $this->$stName;
}
}

The internal variable $this is a reference to the currently implemented object. It is used to reference the class's attributes.

PHP doesn't support access control to member variables and methods of the classes. All the attributes and methods are visible outside the class (i.e public).

PHP also doesn't support method overloading including overloading the class constructor. Method overloading is an Object Oriented concept that allows to create more that one function with the same name, but with different parameter list.

To create an instance of a class (an object), we have to call the class constructor using the new operator. For example:

$obj1=new student("John");

Inheritance is an Object Oriented concept for reusing code. It allows to create a hierarchal relationships between classes. We can create new classes that extends the functionality of existing classes. PHP supports single inheritance only.

To inherit the class student we have to create a new class the extends the student class. For example:

class Tutor extends Student
{
$var workHours;
function Tutor($name,$hours)
{
parent:: Student($name);
$this->$workHours=$hours;
}
function getInfo()
{
return "Name: ". $this->$stName. " and hours :". $this->$workHours;
}
}

The class Tutor inherit the property stName and the method getInfo from Student class. Tutor also has an additional member variable called workHours.

When we inherit a new class from an existing one, we can provide different functionality to this class. This is called method overriding. The method getInfo in Tutor class overrides getInfo method of Student class.

To store objects into a file, they have to be serialized first. The PHP function serialize is used to generate a storable representation of a value. To read the object back from the file, we have to use unserialize method which create a PHP value from a stored representation.

Examples

Example 1: objects.php

In this example, we will demonstrate the use of classes, objects and inheritance with PHP:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"
<html xmlns="
<head>
<title>Using Classes</title>
<script language="php">
class firstClass
{
var $first;
var $second;
function firstClass($fir,$sec)
{
$this->first=$fir;
$this->second=$sec;
}
function sum()
{
return $this->first+$this->second;
}
function setFirst($first)
{
$this->first=$first;
}
function setSecond($second)
{
$this->second=$second;
}
}
class secondClass extends firstClass
{
var $third;
function secondClass($f,$s,$t)
{
parent::firstClass($f,$s);
$this->third=$t;
}
function sum1()
{
return $this->first+$this->second+$this->third;
}
}
</script>
</head>
<body>
<h1 align="center">Using Classes with PHP</h1>
<?php
$val=array(10,20,30);
$obj =new firstClass($val[0],$val[1]);
print $val[0]."+".$val[1]."=";
print $obj->sum();
print "<br />";
$obj->setFirst($val[2]);
print $val[2]."+".$val[1]."=";
print $obj->sum();
print "<br />";
$obj->setsecond($val[0]);
print $val[2]."+".$val[0]."=";
print $obj->sum();
print "<br />";
$obj2=new secondClass($val[0],$val[1],$val[2]);
print $val[0]."+".$val[1]."+".$val[2]."=";
print $obj2->sum1();
print "<br />";
print $val[0]."+".$val[1]."=";
print $obj2->sum();
?>
</body>
</html>

Example 2 serialize.php

This example shows how to write objects to a file and read them back. First we will create a class called Employee in an included file called Employee.inc:

Employee.inc

<?php
class Employee
{
var $first;
var $last;
var $department;
function Employee($f,$l, $d)
{
$this->first=$f;
$this->last=$l;
$this->department=$d;
}
function toString()
{
return "Employee Name :".$this->last . " , ".$this->first. " Employee Department :".$this->department;
}
}
?>

In the following example, we will write objects of Employee to a file and read them back:

serialize.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"
<html xmlns="
<head>
<title>Serializing Objects</title>
</head>
<body>
<h1 align="center">Serializing Objects with PHP</h1>
<?php
include ("Employee.inc");
$obj1 =new Employee( "John","Smith","IT");
$obj2 =new Employee( "Rob","Brown"," Accounting");
$s1=serialize($obj1);
$s2=serialize($obj2);
$hand1=fopen("emp","w");
if($hand1)
{
fputs($hand1,$s1);
fputs($hand1,"\n");
fputs($hand1,$s2);
fclose($hand1);
}
else
{
echo "could not open file";
}
echo "Here are the available Employee records<br/>";
$hand1=fopen("emp","r");
if($hand1)
{
$ss = implode("", @file("emp"));
$es=split("\n",$ss);
for ($i=0;$i<count($es);$i++)
{
$obj = unserialize($es[$i]);
echo $obj->toString()."<br/>";
}
fclose($hand1);
}
else
{
echo "could not open file";
}
?>
</body>
</html>

1