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
Some other articles
Q908 - Object Orientation: Design Patterns Separating detailed data code from the main application - Ruby example Easy data to object mapping (csv and Python) Singleton design pattern - examples and uses Thin application, thick objects - keep you main code simple. Example in Ruby How healthy are the stars of stage and screen? Java Utility class - flexible replacement for array. Also cacheing in objects and multiple catch clauses example. Designing a base class and subclasses, and their extension, in C++ How to avoid too many recalculations within an object Object factories in C++, Python, PHP and Perl Java - factory method, encapsulation, hashmap example Learning to program - what are algorithms and design patterns? Using object orientation for non-physical objects Perl design patterns example Spike solution, refactored and reusable, Python - Example Caching Design Patterns Reading files, and using factories to create vectors of objects from the data in C++ Learning C++ - a design pattern for your first class Design Patterns - what are they? Why use them? What is a factory method and why use one? - Example in Ruby What is a factory? Looking for a practical standards course Object Relation Mapping (ORM) This article H108 - Objects in PHP Associative objects - one object within another. Caching results in an object for efficiency - avoiding re-calculation Singleton design pattern - examples and uses Changing what operators do on objects - a comparison across different programming languages Object factories in C++, Python, PHP and Perl Learning about Object Orientation in PHP - a new set of examples stdClass in PHP - using an object rather than an associative array Objects in PHP - Revision Caching Design Patterns Copying, duplicating, cloning an object in PHP Autoload in PHP How do classes relate to each other? Associated Classes Design Patterns - what are they? Why use them? Designing your application - using UML techniques Computer Graphics in PHP - World (incoming data) to Pixel (screen) conversion Catchable fatal error in PHP ... How to catch, and alternative solutions such as JSON Private and Public - and things between Getting the OO design write - with PHP a example Does copying a variable duplicate the contents? PHP - Object Oriented Design in use What is a factory? The Multiple Inheritance Conundrum, interfaces and mixins Static class members in PHP - a documented example Object Oriented Programming in PHP Shipping a test harness with your class in PHP Serialization - storing and reloading objects Abstract classes, Interfaces, PHP and Java Object Oriented programming - a practical design example PHP4 v PHP5 - Object Model Difference Cleaning up redundant objects When should I use OO techniques? PHP - getclass v instanceof Introduction to Object Oriented Programming Sorting objects in PHP Calling base class constructors Accounts in PHP - an OO demo OO PHP demonstration - comparing objects and more What are factory and singleton classes? Object Oriented Model - a summary of changes from PHP4 to PHP5 Cue the music, I'm happy. Build on what you already have with OO Planning a hotel refurb - an example of a Gant chart in PHP Think about your design even if you don't use full UML This article Don't repeat code - use loops or functions Should I use structured or object oriented? PHP5 lets you say no PHP v Java Object Oriented Programming in PHP