做一些OO PHP。我在第 11 行和第 31 行得到未定义的变量,我不知道为什么。
<?php
class geometricObject {
private $color;
public function __construct($color){
$this->color = $color;
}
public function getColor(){
return $color;
}
public function setColor($color){
$this->color = $color;
}
}
class circle extends geometricObject {
private $radius;
public function __construct($radius, $color){
parent::__construct($color);
$this->radius = $radius;
}
public function getRadius(){
return $radius;
}
public function getArea(){
return (pi() * pow($radius, 2));
}
public function getSurfaceArea(){
return (2 * pi() * $radius);
}
public function setRadius($radius){
$this->radius = $radius;
}
}
?>
<?php
include_once 'templates/classes.php';
$myCircle = new circle(10, "green");
$circleArea = $myCircle->getArea();
$color = $myCircle->getColor();
include_once 'output.php';
?>
<html>
<body>
<?php echo $circleArea; echo $color; ?>
</body>
</html>