Hi guys,
I’m banging my head against the wall trying to figure out why this isn’t working and I was hoping you could help.
I’ve got a child class which is supposed to be sending attributes to its parent class and, though everything looks right to me, I keep getting “Fatal error: Call to undefined function getname() in […]index.php on line 11.” I’ve removed the getName call to see if it’s just that, and worked through the script an item at a time, but the error happens every time it tries to call to the other class.
Here’s the index file that is calling to the parent class (index.php):
[code]<?php
require_once(‘Shape.php’);
require_once(‘Circle.php’);
require_once(‘Square.php’);
$myCircle = new Circle(“aCircle”, 2, 3, 5);
$mySquare = new Square(“aSquare”, 0, 0, 4);
echo $mySquare‐>getName();
echo “
”;
echo "Area of square is ";
echo $mySquare‐>area();
echo “
”;
$mySquare‐>move(23, 45);
echo "Position of square is x = ";
echo $mySquare‐>getX();
echo " y = ";
echo $mySquare‐>getY();
$mySquare‐>setSide(6);
echo “
”;
echo "Area of square is ";
echo $mySquare‐>area();
echo “
”;
echo $myCircle‐>getName();
echo “
”;
echo "Area of circle is ";
echo $myCircle‐>area();
echo “
”;
$myCircle‐>move(15, 20);
echo "Position of circle is x = ";
echo $myCircle‐>getX();
echo " y = ";
echo $myCircle‐>getY();
$myCircle‐>setRadius(‐2);
echo “
”;
echo "Area of circle is ";
echo $myCircle‐>area();
?>[/code]
Here are the files it’s calling to (Square.php):
[code]<?php
require_once(“Shape.php”);
class Square extends Shape {
private $name;
private $side;
function __construct($name, $x, $y, $side) {
$this->name = $name;
Shape::__construct($x, $y);
$this->side = $side;
}
public function setName($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
public function setSide($side) {
if($side < 0) {
$this->side = 0;
}
else {
$this->side = $side;
}
}
public function getSide() {
return $this->side;
}
public function area() {
return $side * $side;
}
}
?>[/code]
and Circle.php
[code]<?php
require_once(“Shape.php”);
class Circle extends Shape {
private $name;
private $radius;
function __construct($name, $x, $y, $radius) {
Shape::__construct($x, $y);
$this->name = $name;
$this->radius = $radius;
}
public function setName($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
public function setRadius($radius) {
if($radius < 0) {
$this->radius = 0;
}
else {
$this->radius = $radius;
}
}
public function getRadius() {
return $this->radius;
}
public function area() {
return $radius * $radius * 3.14159;
}
}
?>[/code]
They, in turn, call on an abstract class defined here (Shape.php):
[code]<?php
abstract class Shape {
private $name;
private $x;
private $y;
function __construct($x, $y) {
$this->x = $x;
$this->y = $y;
}
public function setX($x) {
$this->x = $x;
}
public function getX() {
return $this->x;
}
public function setY($y) {
$this->y = $y;
}
public function getY() {
return $this->y;
}
public function move($xMove, $yMove) {
$this->xMove = $x;
$this->yMove = $y;
}
}
?>[/code]
Any ideas at all are greatly appreciated. I’ve run everything through syntax checkers and found no errors. I’ve also compared my code against example code for similar exercises and it all looks right to me!
Thanks for your help,
-Shawn