Simple Class Extending Problem

Hey everyone. I am starting my first foray into object-oriented PHP, but I am having trouble properly understanding the behavior of ‘extends’. I have been burying my nose in manuals with no luck. Here’s an example of what I’m trying to do:

[php]
class Position {

private $latitude = 0;
private $longitude = 0;

public function getLatitude(){
	return $this->latitude;
}

public function getLongitude(){
	return $this->longitude;
}

}

class Coordinates extends Position {

function __construct($nlat, $nlong){
	$this->latitude = $nlat;
	$this->longitude = $nlong;
}

}

$lat = -123;
$long = 44;

$coordFirst = new Coordinates($lat,$long);

print($coordFirst->getLatitude());
[/php]

I would expect that this would print -123, but it always prints 0 instead. What am I getting wrong here?

Thanks a bunch!

u created $latitude and $longitude of the parent class as private and trying to access from the child class. it probably could be that reason… try changing them to public.

Sponsor our Newsletter | Privacy Policy | Terms of Service