0% found this document useful (0 votes)
4 views

php_qb_program

The document contains various PHP code snippets demonstrating object-oriented programming concepts such as cloning, introspection, serialization, method overriding, magic methods (__call and __callStatic), constructors, destructors, and different types of inheritance (single, multilevel, hierarchical, and multiple through interfaces). Each section provides a brief example of the concept in action, showcasing how classes and objects interact in PHP. Additionally, there is an example of a rectangle class that calculates the area based on length and width properties.

Uploaded by

saeedarwatkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

php_qb_program

The document contains various PHP code snippets demonstrating object-oriented programming concepts such as cloning, introspection, serialization, method overriding, magic methods (__call and __callStatic), constructors, destructors, and different types of inheritance (single, multilevel, hierarchical, and multiple through interfaces). Each section provides a brief example of the concept in action, showcasing how classes and objects interact in PHP. Additionally, there is an example of a rectangle class that calculates the area based on length and width properties.

Uploaded by

saeedarwatkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

1) Cloning 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;

echo "original: <br>";


echo "Name:". $obj1->name."<br>";
echo "Age:".$obj1->age."<br>";

echo "cloned: <br>";


echo "Name:". $obj2->name."<br>";
echo "Age:".$obj2->age."<br>";
?>
2) Introspection
<?php

Trait sampleTrait
{
public function sayHi()
{
echo "Hello from trait";
}
}

interface sampleInterface
{
public function greet();
}

class sampleClass implements sampleInterface


{
use sampleTrait;
public $name="saee";
public $age=19;

public function greet()


{
echo "Hello from Interface";
}

public function show()


{
echo "Hello from class";
}
}

$obj=new sampleClass();

echo "Using get_class():".get_class($obj);


echo "<br>";
echo "<br>";

echo "Using get_class_methods():<br>";


print_r(get_class_methods('sampleClass'));
echo "<br>";
echo "<br>";

echo "Using get_class_vars():<br>";


print_r(get_class_vars('sampleClass'));
echo "<br>";
echo "<br>";

echo "Using class_exists():<br>";


if(class_exists('sampleClass'))
{
echo "Class exists<br>";
}
else
{
echo "Class not exists<br>";
}
echo "<br>";
echo "Using interface_exits():<br>";
if(interface_exists('sampleInterface'))
{
echo "Interface exists<br>";
}
else
{
echo "interface not exists<br>";
}
echo "<br>";

echo "Using trait_exists():<br>";


if(trait_exists('sampleTrait'))
{
echo "Trait exists<br>";
}
else
{
echo "Trait not exists<br>";
}
echo "<br>";
?>
3) Serialization
<?php
$a=serialize(array("welcome”, "to", "php"));
print_r($a."<br>");
$a_un=unserialize($a);
print_r($a_un);
?>

4) Method overriding
<?php

class person
{
public function show()
{
echo "From Parent class";
}
}

class student extends person


{
public function show()
{
echo "From Child 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");
?>

10) Single inheritance


<?php
class parentclass
{
public function display1()
{
echo "from parent class<br>";
}
}
class childclass extends parentclass
{
public function display2()
{
echo "from child class";
}
}
$obj=new childclass();
$obj->display1();
$obj->display2();
?>
11) Multilevel inheritance
<?php
class grandparentclass
{
public function display1()
{
echo "from grandparent class<br>";
}
}
class parentclass extends grandparentclass
{
public function display2()
{
echo "from parent class<br>";
}
}
class childclass extends parentclass
{
public function display3()
{
echo "from child class";
}
}
$obj=new childclass();
$obj->display1();
$obj->display2();
$obj->display3();
?>
12) Hierarchical Inheritance
<?php
class parentclass
{
public function display1()
{
echo "from parentclass class<br>";
}
}
class childclass1 extends parentclass
{
public function display2()
{
echo "from childclass1 class<br>";
}
}
class childclass2 extends parentclass
{
public function display3()
{
echo "from childclass2 class";
}
}
$obj1=new childclass1();
$obj2=new childclass2();

$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();
}

class childclass extends parentclass implements parentinterface


{
public function display2()
{
echo "from parentinterface interface<br>";
}

public function display3()


{
echo "from childclass class";
}
}
$obj1=new childclass();
$obj1->display1();
$obj1->display2();
$obj1->display3();
?>
14)Create a class as “Percentage” with two properties length & width. Calculate area of rectangle
for two objects

<?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();
?>

You might also like