Child class seems not to be sending attributes...

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

What editor are you using to write your php code? In the code you posted here, you are using wrong minus sign, for example here:
[php]echo $mySquare‐>getName();[/php]

There must be normal minus (ASCII code 45), while the sign what you are using is from extended ASCII table (code 226). Here is how correct code look - compare with the code above:
[php]echo $mySquare->getName();[/php]

You have many instances of this wrong char in the index.php, you need to replace them all.

Thank you SO much!

The index file was an example file from the tutorial set I’m working on… the exercise was to take that and work backwards writing the other files to achieve the desired output. In order to test my files, I copy/pasted the code for the given index from a PDF, which must be where I went tragically wrong.

I would never, ever, ever have found that. It’s now totally re-typed by hand and I am able to move forward and figure out the rest of the assignment!

Thanks again,

-Shawn

That’s one sharp eye you got there Php… (or maybe two) :wink:

Red, two as of yet!! :o

Sponsor our Newsletter | Privacy Policy | Terms of Service