Exp 8 PHP

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Name : Shaikh Hassaan

Roll No : 210418
Practical No : 8
1. Write a program for array of objects
<?php
class student{
public $name,$age;
public function __construct($name,$age){
$this->name = $name;
$this->age = $age;
}
public function intro(){
echo "Name : $this->name <br>";
echo "Age : $this->age <br>";
}
}
$std1 = new student("Hassaan",18);
$std2 = new student("Azlan",18);
$std3 = new student("Mohiuddin",18);
$arr = array($std1,$std2,$std3);
foreach($arr as $i){
$i->intro();
}
?>

2. Write a program for method overloading

<?php
class Addition{
function __call($nof , $args){
if($nof == 'Add'){
switch(count($args)){
case 1 :
return $args[0];
break;
case 2 :
return $args[0] + $args[1];
break;
case 3 :
return $args[0] + $args[1] + $args[2];
break;
}
}
}
}
$a = new Addition();
echo $a->Add(2,3)."<br>";
echo $a->Add(2,3,4)."<br>";
echo $a->Add(2);

?>

3. Write a program for method overriding


<?php
class parentC{
function display(){
echo "We are inside the parent class<br>";
}
}
class childC extends parentC{
function display(){
parent::display();
echo "We are inside the child class";
}
}
$p = new parentC();
$c = new childC();
echo $c->display()."<br>";
//echo $p->display()."<br>";

?>
4. Write a program for single level inheritance
<?php
class parentClass{
public $name;
public $age;
function getData($name,$age){
$this->name = $name;
$this->age= $age;
}
}
class childClass extends parentClass{
function display(){
echo "name: $this->name <br> age: $this->age";
}
}
$c = new childClass();
$c->getData("Abu Hassaan","18");
$c->display();
?>

5. Write a program for multi level inheritance


<?php
class dada{
function display(){
echo "Me dada hu<br>";
}
}
class baap extends dada{
function display(){
parent::display();
echo "Me Baap hu<br>";
}
}
class beta extends baap{
function display(){
parent::display();
echo "Me Beta hu<br>";
}
}
$beta = new beta();
$beta->display();
?>

6. Write a program for multiple inheritance


<?php
class student{
public $name,$rollno;
public function getData($name,$rollno){
$this->name = $name;
$this->rollno = $rollno;
}
}
trait marks{
public $m1,$m2,$m3;
public function getMarks($m1,$m2,$m3){
$this->m1= $m1;
$this->m2= $m2;
$this->m3= $m3;
}
}
class display extends student{
use marks;
public function show(){
echo "name: $this->name <br> rollno: $this->rollno <br>";
echo "MAD:$this->m1<br>WBP:$this->m2<br>PWP:$this->m3<br>";
}
}
$d = new display();
$d->getData("Abdul Hassaan",21041318);
$d->getMarks(20,20,20);
$d->show();
?>

7. Write a program for hierarchical inheritance


<?php
class base{
public function display1(){
echo "this is ancestor<br>";
}
}
class child1 extends base{
public function display2(){
echo "This is child 1<br>";
}
}
class child2 extends base{
public function display3(){
echo "This is child 2<br>";
}
}
$c1 = new child1();
$c1->display1();
$c1->display2();
$c2 = new child2();
$c2->display1();
$c2->display3();
?>

8. Write a program for constructor and destructor


<?php
class Student{
public $name,$rno;
function __construct($n,$r){
$this->name = $n;
$this->rno = $r;
echo "$this->name<br>$this->rno";
}
function __destruct(){
echo "<br>Object destroy";
}
//function display()

}
$obj = new Student("Abu Hassaan ",1318);

?>

9. Write a program for abstract class


<?php
abstract class Student{
abstract public function display();
}
class children extends Student{
function display():string{
return "This is an abstract class";
}
}
$obj = new children();
echo $obj->display();
?>

10. Write a program for final keyword with respect to class


<?php
final class Base {

}
class child1 extends Base{

?>

11. Write a program for final keyword with respect to function


<?php
class Base {

final function printdata() {


echo "final base class final method";
}

}
class children extends Base{
function printdata(){
echo "Overriden function";
}

}
$obj = new children();
$obj-printdata();

?>

12. Write a program for static keyword with respect to variable


<?php
function display(){
static $n;
$n++;
echo "$n <br>";
}
display();
display();
display();

?>

13. Write a program for static keyword with respect to function and properties
<?php
class root{
public static $c;

static function count(){


self::$c++;
echo "object ".self::$c." Created<br>";
}

static function display(){


echo "This is static function is called using the classname<br>";
}
static function display1(){
echo "Hello there<br>";
}
static function forChild(){
echo "This is called using the parent keyword<br>";
}

function display2(){
self::display1();
echo "My name is Abdul Hassaan<br>";
}
}

class children extends root{


static function display(){
parent::forChild();
}
}
$obj1= new root();
$obj2 = new children();
root::display();
$obj1->display2();
$obj2->display();

$obj1->count();
$obj1->count();
?>

You might also like