Last active
June 29, 2022 16:57
-
-
Save reaneyk/6604523 to your computer and use it in GitHub Desktop.
Example PHP Abstract Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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
commented
Nov 12, 2019
radius=$r;
}
public function total(){
retutn 3.14*$this->$r*$this->$r;
}
}
$obj = new circle();
$obj->total();
?>
radius=$r;
}
public function total(){
retutn 3.14*$this->$r*$this->$r;
}
}
$obj = new circle();
echo $obj->total();
?>
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