| |||||||||||
| |||||||||||
Associative Objects
Objects in PHP example from a Well House Consultants training course
More on Objects in PHP [link]
Source code: po8 Module: H108
<?php
# Associative Objects - one object in another class time { function __construct($base) { preg_match('/(\d+).+?(\d+)/',$base,&$gotted); $this->hour = $gotted[1]; $this->min = $gotted[2]; $this->mam = $this->hour * 60 + $this->min; } function getforward($by) { if ($by > 0) { $rv = time::format($this->mam + $by); } else { $rv = "untimed"; } return $rv; } function getclock() { $rv = time::format($this->mam); return $rv; } static function format($ends) { $rv = sprintf("%02d:%02d",$ends/60,$ends%60); return $rv; } } class transport { function __construct($s,$where,$when,$dur=-1) { $this->len = 1; $this->where = $where; $this->when = new time($when); $this->seats = $s; $this->dur = $dur; } function getdestiny() { return $this->where; } function gettime() { return $this->when->getclock(); } function getcapacity() { return (floor($this->len * $this->seats)); } function getarrival() { $when = $this->when; return $when->getforward($this->dur); } static function create($raw) { $raw = preg_replace('/#.*/','',$raw); $parts = explode("\t",trim($raw)); $rv = false; if ($parts[0] == "Train") { $rv = new train(floor($parts[2]),floor($parts[3]),$parts[4],$parts[5],floor($parts[6])); } if ($parts[0] == "Bus") { $rv = new bus(floor($parts[2]),$parts[3],$parts[4],floor($parts[5])); } if ($parts[0] == "Plane") { $rv = new plane(floor($parts[2]),$parts[3],$parts[4],floor($parts[5])); } if (! $rv) { if (preg_match('/\S/',$raw)) { # print ("Fault in $raw\n"); throw new Exception('Corrupt line'); } } return $rv; } static function factory($source) { $options = array(); $n=0; foreach (file($source) as $record) { $n++; try { $option = transport::create($record); if ($option) array_push($options,$option); } catch (Exception $e) { print ("OOps in line $n: $record"); } } return $options; } static function getFlow($services) { $so_far = 0; foreach ($services as $service) { $so_far += $service->getCapacity(); } return $so_far; } } class train extends transport { function __construct($len,$s,$where,$when,$dur=-1) { $this->len = $len; $this->where = $where; $this->when = new time($when); $this->seats = $s; $this->dur = $dur; } function getcapacity() { return (floor($this->len * $this->seats * 1.4)); } function getlength() { return ($this->len * 23.0); } function addCarriages($owmany) { return ($this->len += $owmany); } } class bus extends transport { function getcapacity() { return (floor($this->seats + 6)); } function getlength() { return ($this->len * 15.0); } } class plane extends transport { function getlength() { return (79); } } # -------- Above here - code to right of line $travel = transport::factory("podata.txt"); foreach ($travel as $go) { $C = $go->getcapacity(); $L = $go->getlength(); $D = $go->getdestiny(); $W = $go->gettime(); $A = $go->getarrival(); print ("To $D at $W (to $A) for $C length $L\n"); } $escape = transport::getFlow($travel); print ("Total capacity is $escape\n"); /* Sample data Train 180 5 70 Swansea 09-30 58 Train 125 8 65 Cardiff 10:00 93 Train 125 8 65 Swansea 10spock30 55 Bus RMT 84 Old Town 09:40 26 Bus RMT 84 Dorcan 10:10 34 Plane 777 300 Gdansk 10:26 120 Sample run munchkin:dpat grahamellis$ php po8 OOps in line 4: gg To Swansea at 09:30 (to 10:28) for 489 length 115 To Cardiff at 10:00 (to 11:33) for 728 length 184 To Swansea at 10:30 (to 11:25) for 728 length 184 To Old Town at 09:40 (to 10:06) for 90 length 15 To Dorcan at 10:10 (to 10:44) for 90 length 15 To Gdansk at 10:26 (to 12:26) for 300 length 79 Total capacity is 2425 */ 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. 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 |