Practical No 08: Object-Oriented Concepts in PHP
Practical No 08: Object-Oriented Concepts in PHP
Practical No 08: Object-Oriented Concepts in PHP
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.
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;
$mgr->display();
$mgr->display1();
?>
</body>
</html>
<?php
class Employee
{
private $empid = 0;
private $ename = "";
private $salary = 0.0;
<?php
interface A {
function display1();
function display2();
}
class B implements A {
public function display1() {
echo "This is 1st method of interface. </br>";
}
$objb->display1();
$objb->display2();
$objb->display3();
?>
</body></html>
<?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.
trait B {
public function display2()
{
echo "This is trait.</br>";
}
}
class C extends A
{
use B;
private $j;
$objc->display1();
$objc->display2();
$objc->display3();
?>
</body>
</html>
<?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];
}
}
}
}
</body>
</html>
/*
Area of Circle: 14.137155
Area of Rectangle: 41.65
*/
Assessment Scheme
Performance Indicator Weightage
4 Presentation of Output 20 %
Assessment
Date Signature of
Marks Obtained
Subject Teacher
Process Related Product Related
Total (50 Marks)
(30 Marks) (20 Marks)