Categories: Uncategorized

This is Example Code of php Inheritance

This is example code of php inheritance parents and child. extend is the keyword to inherit from the parent class.

 

<?php 

abstract class Shape {

 protected $color;

 public function __construct($color = 'blue')
 {
  $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 = 7;

 public function getArea()
 {
  return .5 * $this->base * $this->height;
 }
}

class Circle extends Shape {

 protected $radius = 5;

 public function getArea()
 {
  return M_PI * pow($this->radius, 2);
 }
}

// long hand methods
// $circle = new Circle;

// echo $circle->getArea();

//shorthand
echo (new Circle)->getArea();
Admin

Recent Posts

Unveiling the Intricacies of the Tinder Algorithm: 5 Surprising Metrics

Monitored by Tinder The inner workings of the Tinder algorithm remain shrouded in secrecy, known…

3 weeks ago

Is Shopping Temu Safe

Use fav19073 30% OFF Shop Now I recently had a fantastic buying experience on Temu,…

4 months ago

Cheap Netflix Subscription

Unlocking Affordable Streaming: How Gamsgo.com is Revolutionizing Netflix Subscriptions Sign Up Save1 coupon code for…

4 months ago

Domains for SALE.

razerkeyb.com gloriousmouse.com

9 months ago

How to Start your own vegetable garden?

Planting your own vegetables and fruit is a great way to save money and eat…

1 year ago

Jobs AI can’t replace

While AI has the potential to automate many tasks, there are still some jobs that…

1 year ago