I have codes below
circle.php
class point{
private $x;
private $y;
function __construct($x, $y){
$this->x = $x;
$this->y = $y;
}
public function getX(){
return $this->x;
}
public function getY(){
return $this->y;
}
}
class circle{
private $x_circle;
private $y_circle;
private $radius;
function __construct($x_circle, $y_circle, $radius){
$this->x_circle = $x_circle;
$this->y_circle = $y_circle;
$this->radius = $radius;
}
public function getCircleX(){
return $this->x_circle;
}
public function getCircleY(){
return $this->y_circle;
}
public function getRadius(){
return $this->radius;
}
public function checkIfInside(){
if(pow(getX() - getCircleX(), 2) + pow(getY() - getCircleY(), 2) < pow(getRadius(),2)){
return true;
} else {
return false;
}
}
}
index.php
include 'circle.php';
$point= new point(3, 4);
$circle= new circle(10, 10, 100);
$circle->checkIfInside();
I try to check if point is inside a circle. With point class we declare point coordinates(x,y). With circle class we declare the coordinates of circle(x,y) and radius. But I get this error: Uncaught Error: Call to undefined function getX()
>Solution :
You need to fix your checkIfInside function. It should receive a point as an argument. Also it lacks $point and $this references when calling methods.
public function checkIfInside($point) {
return pow($point->getX() - $this->getCircleX(), 2) + pow($point->getY() - $this->getCircleY(), 2) < pow($this->getRadius(),2))
}
Also, you should pass the point as an argument in your main code.
include 'circle.php';
$point= new point(3, 4);
$circle= new circle(10, 10, 100);
$circle->checkIfInside($point);