Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Practical No 08: Object-Oriented Concepts in PHP

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Practical No 08: Object-Oriented Concepts in PHP

Practical Outcomes (PrOs):


Write a PHP program to –
a. Inherit members of super class in subclass.
b. Create constructor to initialize object in class.
-- by using Object-Oriented concepts

A. Practical Significance
Object-Oriented Programming (OOP) provides basis for Good Designs, Easier Maintenance
and more Reuse of Code. OOP is a way of coding where objects and classes are used. Objects
represent real world things processes, ideas such as employee, cars, bank accounts etc.
PHP supports number of useful features of OOP. These features are useful in web-based
application development to develop business applications.

B. Minimum Theoretical Background


1. Class and Objects:
Class forms basic for OOP as encapsulation can be achieved by using class as a wrapper.
Objects are the concrete run-time entity which is used to represent information and later objects
communicates with each other.
Syntax of declaring Class in PHP:
class class-name
{
declaration of data-member(s);
definition of member-method(s);
}
Syntax for creating objects:
$object-name = new class-name();

2. Constructor and Destructor:


Constructors are used to initialize objects at the time of creation of that objects. Destructor
is used for housekeeping work. In PHP, __construct() and __destruct() methods are used to write
constructor and destructor respectively inside the class.
Syntax for defining constructor: Syntax for defining destructor:
function __construct() function __destruct()
{ {
// code // code
} }

3. Inheritance:
Creating a new class from existing class is called as Inheritance. In inheritance, a super class is
inherited in sub class.
In php, to inherit a class in another class we use extends keyword.
Syntax of Inheritance
class Super-class {
}
class Sub-class extends Super-class {
}

C. Program Code
a) Write a php program to illustrate inheritance of super class in subclass.
<!DOCTYPE html>
<html>
<body>

<?php
class Employee {
protected $empid;
protected $ename;

public function accept($empid, $ename)


{
$this->empid = $empid;
$this->ename = $ename;
}

public function display()


{
echo "Emp id: " . $this->empid . "</br>";
echo "Name: " . $this->ename . "</br>";
}
}

class Manager extends Employee {


protected $dept;

public function accept1($dept)


{
$this->dept = $dept;
}

public function display1()


{
echo "Dept: " . $this->dept . "</br>";
}
}

$mgr = new Manager();


$mgr->accept(4563, "Shashank");
$mgr->accept1("Marketing");

$mgr->display();
$mgr->display1();
?>

</body>
</html>

b) Write a php program to illustrate creation of constructor to initialize object in class.


<!DOCTYPE html>
<html>
<body>

<?php
class Employee
{
private $empid = 0;
private $ename = "";
private $salary = 0.0;

public function __construct($empid, $ename, $salary) {


$this->empid = $empid;
$this->ename = $ename;
$this->salary = $salary;
}

public function __destruct() {


echo "The object is removed from memory.";
}

public function display() {


echo "Emp id: " . $this->empid . "</br>";
echo "Emp Name: " . $this->ename . "</br>";
echo "Salary: " . $this->salary . "</br>";
}
}

$e1 = new Employee(7855, "Rakesh", 34000.90);


$e1->display();
?>
</body>
</html>
D. Result (Output of Code)
Program a) Program b)
Emp id: 4563 Emp id: 7855
Name: Shashank Emp Name: Rakesh
Dept: Marketing Salary: 34000.90

E. Practical Related Questions


a) Explain how interface is declared and implemented in PHP.
Interface is just like a class but in which only declarations of functions is allowed. We
cannot define any function. All the functions declared in an interface must be defined in the
derived class.
To implement an interface in a class we use implements keyword.
Syntax of declaring and implementing interface:
interface interface-name {
function function1-name();
function function2-name();

function functionN-name();
}

class class-name implements interface-name {


function function1-name() { // code }
function function2-name() { // code }

function functionN-name() { // code }
}
Program for interface:
<!DOCTYPE html>
<html>
<body>

<?php
interface A {
function display1();
function display2();
}
class B implements A {
public function display1() {
echo "This is 1st method of interface. </br>";
}

public function display2() {


echo "This is 2nd method of interface. </br>";
}
public function display3() {
echo "This is the method of class. </br>";
}
}

$objb = new B();

$objb->display1();
$objb->display2();
$objb->display3();
?>
</body></html>

b) What is Abstract class?


An abstract class is a class that contains at least one abstract method. An abstract method is a method
that is declared, but not implemented in the code.
An abstract class or method is defined with the abstract keyword as shown below -
<?php
abstract class ParentClass
{
abstract public function someMethod1();
abstract public function someMethod2($name, $color);
abstract public function someMethod3() : string;
}
?>
When inheriting from an abstract class, the child class method must be defined with the same name,
and the same or a less restricted access modifier. So, if the abstract method is defined as protected,
the child class method must be defined as either protected or public, but not private. Also, the type
and number of required arguments must be the same. However, the child classes may have optional
arguments in addition.
So, when a child class is inherited from an abstract class, we have the following rules:
1. The child class method must be defined with the same name and it redeclares the parent abstract
method
2. The child class method must be defined with the same or a less restricted access modifier
3. The number of required arguments must be the same. However, the child class may have optional
arguments in addition
For example:

<?php
abstract class Car {
public $name;
public function __construct($name) {
$this->name = $name;
}
abstract public function intro() : string;
}
class Audi extends Car {
public function intro() : string {
return "Choose German quality! I'm an $this->name!";
}
}
$audi = new audi("Audi");
echo $audi->intro();
?>

G. Exercise
a) Write a php program to illustrate multiple inheritance using trait.
b) Write a php program to illustrate method overloading.

a) Answer: Program to illustrate multiple inheritance using trait


<!DOCTYPE html>
<html>
<body>
<?php
class A {
private $i;

public function display1() {


$i = 100;
echo "Value is " . $i . "</br>";
}
}

trait B {
public function display2()
{
echo "This is trait.</br>";
}
}

class C extends A
{
use B;

private $j;

public function display3()


{
$j = 700;
echo "Value is " . $j . "</br>";
}
}
$objc = new C();

$objc->display1();
$objc->display2();
$objc->display3();
?>

</body>
</html>

b) Answer: Program to illustrate method overloading


<!DOCTYPE html>
<html>
<body>

<?php
class Shape {
public function __call($name_of_function, $arguments) {
if($name_of_function == 'area') {
switch(count($arguments)) {
case 1:
return 3.14159 * $arguments[0];
case 2:
return $arguments[0] * $arguments[1];
}
}
}
}

$obj_shape = new Shape();

echo "Area of Circle: " . $obj_shape->area(4.5) . "</br>";


echo "Area of Rectangle: " .
$obj_shape->area(8.5, 4.9) . "</br>";
?>

</body>
</html>

/*
Area of Circle: 14.137155
Area of Rectangle: 41.65
*/
Assessment Scheme
Performance Indicator Weightage

Process Related (30 Marks) 60 %


Write appropriate code to generate desired output
1 30 %
in Web application.
2 Debug, Test and Execute the programs 20 %

3 Following Ethical Practice 10 %

Product Related (20 Marks) 40 %

4 Presentation of Output 20 %

5 Timely Submission of Practical Assignment 10 %

6 Able to answer the sample questions 10 %

Total (50 Marks) 100 %

List of Students / Team Members


1) ………………………………………………………………………………….
2) ………………………………………………………………………………….
3) ………………………………………………………………………………….
4) ………………………………………………………………………………….

Assessment
Date Signature of
Marks Obtained
Subject Teacher
Process Related Product Related
Total (50 Marks)
(30 Marks) (20 Marks)

You might also like