| |||||||||||
| |||||||||||
A set of classes in PHP, factory method, comparators and test harness
Object Orientation: Composite Objects example from a Well House Consultants training course
More on Object Orientation: Composite Objects [link]
Source code: doggies.php Module: Q909
<?php
class animal { private $years; private $bpd; // Private - accessible ONLY from within the class // So you can changes the internal structure // later one - e.g. store in days (or years // short of 37 ... protected $called; // Protected - accessible within the class so that // it can used internally within the class, and also // from other classes that are derived from it, but // not directly from outside the extended class tree // Public - accessible from inside and out; needs to // be maintained as a stable API in case customers of // the class use it within their code // Note - undeclared variables within an instance of an // object in PHP default to being public, as do any which // are declared with "var" function __construct($name,$age,$noise) { $this->called = $name; $this->years = $age; $this->bpd = $noise; } function getyears() { return $this->years; } function getbarks() { return $this->years * $this->bpd * 365; } function getname() { return $this->called; } function noisier($that) { $b1 = $this->getbarks(); $b2 = $that->getbarks(); if ($b1 < $b2) return $that; return $this; } function noisiest($choices) { // Static Method $sofar = $choices[0]; foreach ($choices as $maybe) { $sofar = $sofar->noisier($maybe); } return $sofar; } function factory($about) { // Indirect constructor call $shards = explode(" ",$about); if ($shards[0] == "canine") { return new dog($shards[1], $shards[2], $shards[3]); } else { // etc } } } class dog extends animal { } class sealion extends animal { function __construct($name,$age) { parent::__construct($name,$age,20); }} class silentanimal extends animal { function __construct($name,$age) { parent::__construct($name,$age,0); }} class giraffe extends silentanimal {} class cat extends silentanimal { function getname() { return $this->called . " (a cat!)"; } } # ------- main (test) code -------- /* The test code is off to one side like this so that it's only going to run when the PHP file is called up, stand alone, from the command line. This way we can supply and maintain a test harness / piece of example code in the same file that our classes are maintained within. */ if ($_SERVER[SCRIPT_FILENAME] == __FILE__) { // Setup a whole lot of objects in various ways for this test (in a live // application, data would probably come from a file or database and be // generated by a loop of calls to a factory method) $woof = new dog("Hamlet",8,2); $tall = new giraffe("Tiny",11); $yap = new dog("Gypsy",4,0.01); $pest = new dog("Chester",9,150); $miaow = new cat("Charlie",14); $pack = array($woof, $yap, $pest, animal::factory("canine Gnipper 1 4"), $geo = new sealion("George",6), new sealion("Ethel",8), $tall, $miaow); foreach ($pack as $mydaug) { $name = $mydaug->getname(); $bsf = $mydaug->getbarks(); print ("$name has barked $bsf times so far. "); print ("Age is " . $mydaug->getyears() . ".\n"); } // Noisier of $geo and $woof /* 1. Compare in the app? Yuk, Yuk! 2. Write a function to compare? Yuk e.g. $bbb = noisier($geo,$woof); 3. Write a method to compare Yukfree ! e.g. $bbb = $geo->noisier($woof); */ $bbb = $geo->noisier($woof); print ($bbb->getname() . " is a bit LOUDer\n"); $ccc = animal::noisiest($pack); print ($ccc->getname() . " is a PAIN\n"); } /* munchkin:hound grahamellis$ php doggies.php Hamlet has barked 5840 times so far. Age is 8. Gypsy has barked 14.6 times so far. Age is 4. Chester has barked 492750 times so far. Age is 9. Gnipper has barked 1460 times so far. Age is 1. George has barked 43800 times so far. Age is 6. Ethel has barked 58400 times so far. Age is 8. Tiny has barked 0 times so far. Age is 11. Charlie (a cat!) has barked 0 times so far. Age is 14. George is a bit LOUDer Chester is a PAIN munchkin:hound grahamellis$ */ ?> Learn about this subject
This module and example are covered on the following public courses:
* Learning to program in Python * Learning to Program in Java * Learning to program in Ruby * Learning to program in C and C++ * Object Oriented Programming in PHP * Python Programming * Perl for Larger Projects * C++ for C Programmers * C and C++ Programming * Java Bootcamp * Ruby Programming * Java Programming for the Web * Intermediate Python * Learning to program in C and C++ * C and C++ Programming Also available on on site courses for larger groups Books covering this topic
Yes. We have over 700 books in our library. Books
covering all the subjects we teach are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "Object Orientation: Composite Objects" training module. You'll find a description of the topic and some
other closely related examples on the "Object Orientation: Composite Objects" module index page.
Full description of the source code
You can learn more about this example on the training courses listed on this page,
on which you'll be given a full set of training notes.
Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License. Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre. • The Horse's mouth provides a daily tip or thought. • Further resources are available via the resources centre. • All of these resources can be searched through through our search engine • And there's a global index here. Web site author
Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job. Remember that some of our examples show you how not to do
things - check in your notes. Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details. Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request. |
| ||||||||||
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho PAGE: http://www.wellho.net/resources/ex.php • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb |