该页面无法处理请求。 运行 php 在本地主机上

The page is unable to handle request. running php on localhost

我是 运行 本地主机上的这段代码。在 http://localhost:8000/Polymorphism.php 上 运行 时出现错误。这是 php 上的简单多态代码。其他代码很容易运行但是有错误运行这段代码

此页面不工作本地主机当前无法处理此请求。 HTTP 错误 500

<?php


public interface Shape{

   public function calculateArea();


} 

 public class Circle implements Shape{

  private $radius;


  public function __construct($r){
   $this->radius=$r;
  }

  public function calculateArea(){

  echo 'Area of circle = '.pi()* $this->radius*$this->radius.'<br>';

  }
}

class Rectangle implements Shape{

 private $height;
 private $width;

 public function __construct($h,$w){
  this->height=$h;
  this->width=$w;
 }

 public function calculateArea(){
 echo 'Area of a Rectangle=' .$this->height.$this->width.'<br>';
 }

 }

 $circle= new Circle(5);
 $rect= new Rectangle(10,20);


 $circle->calculateArea();
 $rect->calculateArea();


?>

您永远不会将访问修饰符分配给 classes 或 interfaces.They 仅用于特定的方法和属性。你在 class Rectangle 中还有两个错误,你应该在

中提到高度和宽度
  $this->height=$h;
  $this->width=$w;

将您的整体代码更改为

<?php
interface Shape{

 public function calculateArea();

} 

class Circle implements Shape{

  private $radius;


  public function __construct($r){
   $this->radius=$r;
 }

 public function calculateArea(){

  echo 'Area of circle = '.pi()* $this->radius*$this->radius.'<br>';

}
}

class Rectangle implements Shape{

 private $height;
 private $width;

 public function __construct($h,$w){
  $this->height=$h;
  $this->width=$w;
}

public function calculateArea(){
 echo 'Area of a Rectangle=' .$this->height.$this->width.'<br>';
}

}

$circle= new Circle(5);
$rect= new Rectangle(10,20);

$circle->calculateArea();
$rect->calculateArea();
?>