Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Two classes, inheritance, factory, JSON, error handler, etc
Objects in PHP example from a Well House Consultants training course
More on Objects in PHP [link]

This example is described in the following article(s):
   • Catchable fatal error in PHP ... How to catch, and alternative solutions such as JSON - [link]

Source code: oop5.php Module: H108

class stop {

        function busier($that) {
                $first = $this->gettickets(2010);
                $second = $that->gettickets(2010);
                if ($first > $second) return $this;
                return $that;

        function __construct($record) {
                $this->info = trim($record);
                $this->cached = 0;
        protected function checkcache() {
                if ($this->cached == 0) {
                        $this->cached = 1;
                        $this->fields = explode("\t",$this->info);

        static function busiest($group) {
                $sofar = $group[0];
                foreach ($group as $maybe) {
                        $sofar = $sofar->busier($maybe);
                return $sofar;

        static function factory($stringy) {
                $fol = explode("\t",$stringy);
                if (count($fol) < 6)
                        return new ferryport($stringy);
                return new station ($stringy);


class station extends stop {
        function getname() {
                return $this->fields[6];
        function gettickets($year) {
                return ($this->fields[$year-1998]);

class ferryport extends stop {
        function getname() {
                return $this->fields[2];
        function gettickets($year) {
                return ($this->fields[3]);


function cat_chit($errno, $errstr, $errfile, $errline) {
  if ( E_RECOVERABLE_ERROR===$errno ) {
    print ("\n\ncaught a normlly fatal error\n");
    print ("Error number: $errno\n");
    print ("Error string: $errstr\n");
    print ("Error file: $errfile\n");
    print ("Error line: $errline\n");
    return true;
  return false;

/* Sample Data record
8976 ABD AB11 6LX NJ 941058 57.1430482477 -2.0974804963
Aberdeen 1931973 2107855 2278872 2470281 2568810 2657014 */

$fh = fopen("railstats.txt","r");

$destinations = array();
while (($lyne = fgets($fh)) != "" ) {
        array_push($destinations,new station($lyne));

array_push($destinations, new ferryport("0000\tZEX\tExmouth_Slipway\t14107"));
$destinations[500] = new ferryport("0001\tXDO\tDover_Dox\t1234568");

array_push($destinations, stop::factory("0001\tXCA\tCalais\t995423"));

foreach ($destinations as $current) {
        $nos1 = $current->getname();
        $older = $current->gettickets(2005);
        $newer = $current->gettickets(2010);
        print ("Set up $nos1 - from $older to $newer\n");

$result = $destinations[50]->busier($destinations[500]);
print ($result->getname() . "\n");

$result = stop::busiest(array_slice($destinations,100,200));
print ($result->getname() . "\n");


# Following line would fail - can't convert object to string in PHP
print ($result . "\n");

print (json_encode($result) . "\n");

$saveit = <<<SAMPLE

Here is some sample output:

Set up Aylesbury Vale Parkway - from NULL to 49212
Set up Abercynon North - from 112811 to NULL
Set up Garston (Merseyside) - from 190778 to NULL
Set up Exmouth_Slipway - from 14107 to 14107
Set up Calais - from 995423 to 995423

caught a normlly fatal error
Error number: 4096
Error string: Object of class station could not be converted to string
Error file: /Users/grahamellis/oop/oop5.php
Error line: 108

{"info":"5268\tBTN\tBN1 3XP\tTQ 309050\t50.8289532182\t-0.141225193\tBrighton
"fields":["5268","BTN","BN1 3XP","TQ 309050","50.8289532182","-0.141225193",



Learn about this subject
This module and example are covered on the following public courses:
 * Object Oriented Programming in PHP
 * PHP Programming
 * Learning to program in PHP
Also available on on site courses for larger groups

Books covering this topic
Yes. We have over 700 books in our library. Books covering PHP 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 "Objects in PHP" training module. You'll find a description of the topic and some other closely related examples on the "Objects in PHP" 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.

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.

Web site author
This web site is written and maintained by Well House Consultants.

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.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2022: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
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