php_programs_All
php_programs_All
<?php
class sample
public $name;
$this->name=$n1;
$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;
$this->len=$l;
$this->wid=$w;
$p=2*($this->len+$this->wid);
$a=$this->len*$this->wid;
$obj->cal(2,2);
$obj->perimeter();
$obj->area();
?>
3) Constructor
<?php
class sample
?>
4) Parameterized Constructor
<?php
class sample
public $name;
?>
5) Destructor
<?php
class sample
{
echo "Hello from Destructor";
?>
6) Method Overriding
<?php
class student
$p=new person();
$p->details();
?>
7) __call() function
<?php
class sample
{
public function __call($name,$arguments)
echo $arg."<br>";
$obj=new sample();
$obj->greet("Hello","Hii","Hiii");
?>
8) __callStatic() function
<?php
class sample
echo $arg."<br>";
sample::greet("Hello","Hii","Hiii");
?>
9) Single Inheritance
<?php
class parent1
$c=new child1();
$c->show();
$c->display();
?>
<?php
class grandparent
$obj=new child();
$obj->display1();
$obj->display2();
$obj->display3();
?>
<?php
class parent1
$obj=new child1();
$obj1=new child2();
$obj->display1();
$obj->display2();
$obj1->display3();
?>
<?php
interface parent1
}
interface parent2
$obj=new child();
$obj->display1();
$obj->display1();
$obj->display3();
?>
<?php
trait parent1
{
public function display1()
trait parent2
class child
use parent1,parent2;
$obj=new child();
$obj->display1();
$obj->display1();
$obj->display3();
?>
14) Introspection
<?php
trait sampletrait
{
interface sampleinterface
use sampletrait;
public $name="saee";
public $age=20;
$obj=new sampleclass();
echo "<br>";
echo "<br>";
print_r(get_class_vars('sampleclass'));
echo "<br>";
echo "<br>";
print_r(get_class_methods('sampleclass'));
echo "<br>";
echo "<br>";
if(class_exists('sampleclass'))
echo"Class exists";
else
echo "<br>";
echo "<br>";
if(interface_exists('sampleinterface'))
echo"Interface exists";
else
echo "<br>";
echo "<br>";
echo "using trait_exists() function";
if(trait_exists('sampletrait'))
echo"Trait exists";
else
?>
Output:
<?php
class sample
public $name;
public $age;
$this->name=$n;
$this->age=$a;
}
$obj1=new sample("Saee",19);
$obj2=clone $obj1;
echo "Name:".$obj1->name."<br>";
echo "Age:".$obj1->age."<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);
?>