<?php
declare(strict_types=1);
class Ball{
protected const PI = 3.14;
protected float $radius;
private string $name;
public function __construct(float $r,string $n="Ball"){
$this->radius=$r;
$this->name=$n;
}
public function getVolume(): float{
return (4/3)*self::PI*($this->radius**3);
}
public function getFieldOfSurface(): float{
return 4*self::PI*($this->radius**2);
}
}
class Circle extends Ball{
private string $name;
public function __construct(string $n="Circle"){
$this->name=$n;
}
public function getCircuit(): float{
return 2*self::PI*$this->radius;
}
public function getField(): float{
return self::PI*($this->radius**2);
}
}
$ball = new Ball(9.5);
$circle = new Circle();
print_r($ball);
print_r($circle);
var_dump($ball->getVolume());
var_dump($ball->getFieldOfSurface());
var_dump($circle->getCircuit());
var_dump($circle->getField());
?>
Chcę napisać program gdzie klasa Circle dziedziczy po klasie ball promień i liczy dla siebie Obwód i Pole a dla Kuli objętość i pole powierzchni. Kod działa tylko dla funkcji klasy Ball a dla funkcji dla klasy Circle wywala taki błąd: Fatal error: Uncaught Error: Typed property Ball::$radius must not be accessed before initialization in D:\php\oop6.php:25
Stack trace:
#0 D:\php\oop6.php(37): Circle->getCircuit()
#1 {main}
thrown in D:\php\oop6.php on line 25