1)Write a PHP script to demonstrate the introspection for examining class(use function get_declared_classes() ,get_class_methods() and get_class_vars()).
intro.php-Method
1
<?php
class
Myclass
{
public $a;
public $b=1;
public $c='Archana';
function Myclass()
{
//Myclass function
}
function myfun1()
{
//functin
}
function myfun2()
{
//functin
}
}
$class=get_declared_classes();
foreach($class as $cname)
{
echo"$cname<br>";
}
echo"<br>Class Methods
are : <br>";
$m=get_class_methods('Myclass');
foreach($m as $mname)
{
echo"$mname<br>";
}
$cp=get_class_vars('Myclass');
echo"class variables are
:<br>";
foreach($cp as $cpname => $v)
{
echo"$cpname : $v
<br>";
}
?>
Method 2
<?php
class
myclass
{
//
constructor
function myclass()
{
return(true);
}
// method 1
function myfunc1()
{
return(true);
}
// method 2
function myfunc2()
{
return(true);
}
}
echo
"<b>get_class_methods example:-</b><br>";
$class_methods
= get_class_methods('myclass');
//
or
$class_methods
= get_class_methods(new myclass());
foreach
($class_methods as $method_name) {
echo "$method_name\n<BR>";
}
?>
<?php
class
myclass1
{
var $var1; // this has no default
value...1 get_class_vars() example
var $var2 = "xyz";
var $var3 = 100;
private $var4;
// constructor
function myclass1() {
// change some properties
$this->var1 = "foo";
$this->var2 = "bar";
return true;
}
}
echo
"<b>get_class_vars:-</b><br>";
$my_class
= new myclass1();
$class_vars =
get_class_vars(get_class($my_class));
foreach ($class_vars as $name => $value)
{
echo "$name : $value\n
<BR>";
}
?>
<?php
echo
"<b>get_declared_classes:-</b><br>";
print_r(get_declared_classes());
?>
2) Derive aclass square from class
Rectangle. Create one more class circle. Create an interface with only one
method called area (). Implement this interface in all the classes. Include
appropriate data members and constructors in all classes.Write a program to
accept details of a square, circle and rectangle and display the area.
slip3.html
<html>
<body>
<form
action="slip3.php" method="get">
<h2> Square :
</h2>
Enter
side for Square : <input type="text" name=s>
<h2> Rectangle :
</h2>
Enter
length : <input type="text"
name=l> <br>
Enter
breadth : <input type="text" name=b>
<h2> Circle :
</h2>
Enter
radius : <input type="text" name=r> <br>
<input type="submit" name=submit
value=Submit>
<input
type="reset" name=reset value=reset>
</form>
</body>
</html>
slip3.php
<?php
$s=$_GET['s'];
$l=$_GET['l'];
$b=$_GET['b'];
$r=$_GET['r'];
interface
findarea
{
function
area($l,$c);
}
class
rectangle implements findarea
{
function
area($l,$b)
{
$area =
$l*$b;
echo
"Area of Rectanle :".$area."<br>";
}
}
class
square extends rectangle
{
function
area($s,$s1)
{
$area =
$s*$s1;
echo
"Area of Square :". $area."<br>";
}
}
class circle
{
function
area($r,$r1)
{
$area =
0.5*$r*$r1;
echo
"Area of Circle :". $area."<br>";
}
}
$fr
= new rectangle;
$fr->area($l,$b);
$fs = new
square;
$fs->area($s,$s);
$fc = new
circle();
$fc->area($r,$r)
?>