Main Content

North, Norther and Northest - PHP 5 Objects

Archive - Originally posted on "The Horse's Mouth" - 2005-11-04 18:15:45 - Graham Ellis

Here's a PHP example of calls to PHP methods to return the Longitude North of a place, the more northerly of two objects via an object (dynamic) method, and the most northerly of a whole array of objects via a static function.

$north = $ilike[17]->getlong();

$norther = $ilike[45]->northof($ilike[41]);

$northest = place::northmost($ilike);


To return a single longitude, we've used a property access method of the sort you'll have written a hundred times if you're an OO programmer.

To compare two objects, we've elected to run a method on one of the objects and pass the other in as the parameter; this method returns the more northerly object:

function northof($that) {
if ($this->lat > $that->lat) return $this;
return $that;}


Comparing a whole list of objects, our static method reads:

function northmost($these) {
$sofar = $these[0];
for ($k=1; $k if ($these[$k]->lat > $sofar->lat)
$sofar = $these[$k];
}
return $sofar;
}


Although this code was written and tested with PHP 5.0.5, I see no reason why these code snippets won't work in PHP 4.

See the complete application and complete classes file in our training modules area