1) Class and Object
<?php
class sample
public $name;
public function setdata($n1)
$this->name=$n1;
public function getdata()
echo "Your name is ".$this->name;
$obj=new sample();
$obj->setdata("Saee");
$obj->getdata();
?>
Output:
2) WAP to create a class rectangle with two properties length and width. Calculate perimeter and area of rectangle.
<?php
class Rectangle
{
public $len,$wid;
public function cal($l,$w)
$this->len=$l;
$this->wid=$w;
public function perimeter()
$p=2*($this->len+$this->wid);
echo "Perimeter is $p";
public function area()
$a=$this->len*$this->wid;
echo "Area is $a";
$obj= new Rectangle();
$obj->cal(2,2);
$obj->perimeter();
$obj->area();
?>
3) Constructor
<?php
class sample
public function __construct()
{
echo "Hello from Constructor.";
$obj= new sample();
?>
4) Parameterized Constructor
<?php
class sample
public $name;
public function __construct($n)
echo "Hello ".$this->name=$n;
$obj= new sample("Saee");
?>
5) Destructor
<?php
class sample
public function __construct()
echo "Hello From Constructor";
public function __destruct()
{
echo "Hello from Destructor";
$obj= new sample();
?>
6) Method Overriding
<?php
class student
public function details()
echo "This is from student class";
class person extends student
public function details()
echo "This is from person class";
$p=new person();
$p->details();
?>
7) __call() function
<?php
class sample
{
public function __call($name,$arguments)
echo "Method $name is not defined<br>";
echo "Arguments are:<br>";
foreach ($arguments as $arg)
echo $arg."<br>";
$obj=new sample();
$obj->greet("Hello","Hii","Hiii");
?>
8) __callStatic() function
<?php
class sample
public static function __callStatic($name,$arguments)
echo "Method $name is not defined<br>";
echo "Arguments are:<br>";
foreach ($arguments as $arg)
echo $arg."<br>";
sample::greet("Hello","Hii","Hiii");
?>
9) Single Inheritance
<?php
class parent1
public function show()
echo "This is from class Parent<br>";
class child1 extends parent1
public function display()
echo "This is from class Child";
$c=new child1();
$c->show();
$c->display();
?>
10) Multilevel Inheritance
<?php
class grandparent
public function display1()
echo "Hello from GrandParent class.<br>";
}
class parent1 extends grandparent
public function display2()
echo "Hello from Parent class.<br>";
class child extends parent1
public function display3()
echo "Hello from Child class.<br>";
$obj=new child();
$obj->display1();
$obj->display2();
$obj->display3();
?>
11) Hierarchical Inheritance
<?php
class parent1
public function display1()
echo "Hello from Parent class.<br>";
}
class child1 extends parent1
public function display2()
echo "Hello from Child class 1.<br>";
class child2 extends parent1
public function display3()
echo "Hello from Child class 2.<br>";
$obj=new child1();
$obj1=new child2();
$obj->display1();
$obj->display2();
$obj1->display3();
?>
12) Multiple Inheritance (Interface)
<?php
interface parent1
public function display1();
}
interface parent2
public function display2();
class child implements parent1,parent2
public function display1()
echo "Hello from Interface parent 1<br>";
public function display2()
echo "Hello from Interface parent 2<br>";
public function display3()
echo "Hello from Class child.<br>";
$obj=new child();
$obj->display1();
$obj->display1();
$obj->display3();
?>
13) Multiple Inheritance (Trait)
<?php
trait parent1
{
public function display1()
echo "Hello from Trait parent 1<br>";
trait parent2
public function display2()
echo "Hello from Trait parent 2<br>";
class child
use parent1,parent2;
public function display3()
echo "Hello from Class child.<br>";
$obj=new child();
$obj->display1();
$obj->display1();
$obj->display3();
?>
14) Introspection
<?php
trait sampletrait
{
public function display1()
echo "Hello from Trait";
interface sampleinterface
public function display2();
class sampleclass implements sampleinterface
use sampletrait;
public $name="saee";
public $age=20;
public function display3()
echo "Hello from class";
public function display2()
echo "Hello from Interface";
$obj=new sampleclass();
echo "using get_class() function".get_class($obj);
echo "<br>";
echo "<br>";
echo "using get_class_vars() function";
print_r(get_class_vars('sampleclass'));
echo "<br>";
echo "<br>";
echo "using get_class_methods() function";
print_r(get_class_methods('sampleclass'));
echo "<br>";
echo "<br>";
echo "using class_exists() function";
if(class_exists('sampleclass'))
echo"Class exists";
else
echo "Class not exists";
echo "<br>";
echo "<br>";
echo "using interface_exists() function";
if(interface_exists('sampleinterface'))
echo"Interface exists";
else
echo "Interface not exists";
echo "<br>";
echo "<br>";
echo "using trait_exists() function";
if(trait_exists('sampletrait'))
echo"Trait exists";
else
echo "Trait not exists";
?>
Output:
15) Cloning Object
<?php
class sample
public $name;
public $age;
public function __construct($n,$a)
$this->name=$n;
$this->age=$a;
}
$obj1=new sample("Saee",19);
$obj2=clone $obj1;
echo "Before cloning:<br>";
echo "Name:".$obj1->name."<br>";
echo "Age:".$obj1->age."<br>";
echo "After cloning:<br>";
echo "Name:".$obj2->name."<br>";
echo "Age:".$obj2->age."<br>";
?>
16) Serialization
<?php
$a=serialize(array("hello","world"));
print_r($a."<br>");
$a_un=unserialize($a);
print_r($a_un);
?>