-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinheritance.php
More file actions
64 lines (56 loc) · 1.57 KB
/
inheritance.php
File metadata and controls
64 lines (56 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
class Base {
public function getEyeColor() {
return "Blue";
}
}
class Derived extends Base {
}
$obj = new Derived();
abstract class Shape {
protected $color;
public function __construct($color = 'red') {
$this->color = $color;
}
public function getColor() {
return $this->color;
}
abstract protected function getArea();
}
class Square extends Shape {
protected $length = 4;
public function getArea() {
return pow($this->length, 2);
}
}
class Triangle extends Shape {
protected $base = 4;
protected $height = 10;
public function getArea() {
return (0.5 * $this->base * $this->height);
}
}
class Circle extends Shape {
protected $radius = 5;
public function getArea() {
return pi() * pow($this->radius, 2);
}
}
$square = new Square();
$triangle = new Triangle();
$circle = new Circle();
?>
<html>
<head>
<title>Inheritance</title>
</head>
<body>
<h3>Eye Color: <?php echo $obj->getEyeColor(); ?></h3>
<hr>
<h3>Color: <?php echo $square->getColor(); ?></h3>
<h3>Area of Sqaure: <?php echo $square->getArea(); ?></h3>
<h3>Area of Triangle: <?php echo $triangle->getArea(); ?></h3>
<h3>Area of Circle: <?php echo $circle->getArea(); ?></h3>
<!-- <h3>Area of Triangle: <?php echo (new Triangle())->getArea(); ?></h3> -->
</body>
</html>