php_qb_program
php_qb_program
<?php
class demo
{
public $name, $age;
public function __construct($n, $a)
{
$this->name=$n;
$this->age=$a;
}
}
$obj1=new demo("Saee",19);
$obj2=clone $obj1;
Trait sampleTrait
{
public function sayHi()
{
echo "Hello from trait";
}
}
interface sampleInterface
{
public function greet();
}
$obj=new sampleClass();
4) Method overriding
<?php
class person
{
public function show()
{
echo "From Parent class";
}
}
$obj=new student();
$obj->show();
?>
5) __call() method
<?php
class demo
{
public function __call($name, $argument)
{
echo "Method $name is not defined.<br>";
echo "Arguments are:<br>";
foreach($argument as $arg)
{
echo $arg."<br>";
}
}
}
$obj=new demo();
$obj->greet("hello", "hi");
?>
6) __callStatic() method
<?php
class demo
{
public static function __callStatic($name, $argument)
{
echo "Method $name is not defined.<br>";
echo "Arguments are:<br>";
foreach($argument as $arg)
{
echo $arg."<br>";
}
}
}
$obj=new demo();
demo::greet("hello", "hi");
?>
7) Constructor and default constructor
<?php
class student
{
public function __construct()
{
echo "I am a student";
}
}
$obj=new student();
?>
8) Destructor
<?php
class demo
{
public function __construct()
{
echo "I am in constructor<br>";
}
public function __destruct()
{
echo "I am in destructor";
}
}
$obj=new demo();
?>
9) Parameterized constructor
<?php
class student
{
public $name;
public function __construct($n)
{
$this->name=$n;
echo "hello ".$this->name;
}
}
$obj=new student("saee");
?>
$obj1->display1();
$obj1->display2();
$obj2->display3();
?>
13) Multiple inheritance through interface
<?php
class parentclass
{
public function display1()
{
echo "from parentclass class<br>";
}
}
interface parentinterface
{
public function display2();
}
<?php
class rectangle
{
public $length, $width;
public function __construct($l, $w)
{
$this->length=$l;
$this->width=$w;
}
public function area()
{
$area=$this->length*$this->width;
echo "Area of rectangle is:".$area."<br>";
}
}
$obj1=new rectangle(2,2);
$obj2=new rectangle(3,3);
$obj1->area();
$obj2->area();
?>