Skip to content

Instantly share code, notes, and snippets.

@reaneyk
Last active June 29, 2022 16:57
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reaneyk/6604523 to your computer and use it in GitHub Desktop.
Save reaneyk/6604523 to your computer and use it in GitHub Desktop.
Example PHP Abstract Class
<?php
//Define a new Abstract class for all 'shapes' to extend
abstract class Shape {
//Define the methods required for classes to extend
//the abstract class
abstract protected function getColor();
abstract protected function setColor($color);
//Common function available to all classes extending the Shape class
public function describe() {
return sprintf("I'm an %s %s\n", $this->getColor(), get_class($this));
}
}
//Define a new 'Triangle' class that extends the
//'Shape' abstract class
class Triangle extends Shape {
private $color = null;
//Define the required methods defined in the abstract
//class 'Shape'
public function getColor() {
return $this->color;
}
//Note that the method signature matches the abstract
// class with only one parameter
public function setColor($color) {
$this->color = $color;
}
}
//Instantiate the Triange class
$triangle = new Triangle();
//Set the color
$triangle->setColor('Orange');
//Print out the value of the describe common method
//provided by the abstract class
//Will print out out "I'm an Orange Triange"
print $triangle->describe();
@rajatgupta0303
Copy link

radius=$r; } public function total(){ retutn 3.14*$this->$r*$this->$r; } } $obj = new circle(); $obj->total(); ?>

@rajatgupta0303
Copy link

radius=$r; } public function total(){ retutn 3.14*$this->$r*$this->$r; } } $obj = new circle(); echo $obj->total(); ?>

@rajatgupta0303
Copy link

radius=$r; } public function total(){ return 3.14*$this->radius * $this->radius; } } $obj = new circle(); echo $obj->total(); ?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment