Exercises, examples and other material relating to training module H108. This topic is presented on public courses
As your PHP application grows, you'll want to use the same logic on many pages without having to duplicate it, and you'll want to group together all the code that refers to a particular type of data (also known as a class of object). PHP has good support for objects as from release 4, and that support is greatly enhanced from release 5.
|Articles and tips on this subject
|Associative objects - one object within another.
A new demonstration of the use of objects within objects - [here] from today's PHP course.
Faced with a class of public transport departure times (as strings) and run times (in pinutes), I added a class of time objects so that I could encapsulate the calculations of time into their own class.
|Caching results in an object for efficiency - avoiding re-calculation
Do you like to do the same thing time and time and time again? Probably not, as it's boring and inefficient. Computers don't get bored, but they can be inefficient if not programed well ... and because they don't get bored, they don't complain if they're inefficient.
Imagine you have a class of ...
|Singleton design pattern - examples and uses
A Singleton class is one where you can only create one object of a type, or a second object is really a share of the first. An example in PHP of a Singleton design pattern - [here]. Uses a static (class) variable to flag object creation.
A singleton is especially sueful when you're looking to hold ...
|Changing what operators do on objects - a comparison across different programming languages
In Object Oriented languages, you used named pieces of code known as methods to perform an operation on a variable - for example you might write
to take a list of (something) for a week and add a list relating to weekend onto the end of it.
Sometimes, the method ...
|Object factories in C++, Python, PHP and Perl
Raw materials are converted into a manufactured product in a factory or by a factory procedure, and the sort of product you can make depends on both the raw material that's supplied and on the capability of the factory.
And so it is with Object Oriented Computer Programming - supply raw material ...
|When should I use OO techniques?
We have two friends. They're married. She's a keen advocate of Object Oriented Programming and he can't stand the approach. So take them to the pub, buy them a couple of drinks each, mention OO programming and watch the feathers fly ;-)
Seriously, Object Oriented Programming has its place - and there ...
|Learning about Object Orientation in PHP - a new set of examples
I've written a new series of examples to teach Object Oriented Programming in PHP ... taking tables in a restaurant as my example, as we all have to eat. Calculating things like the number of people who can sit at a table (that's not as straightforward as you think) and hiding that calculation in the ...
|stdClass in PHP - using an object rather than an associative array
An object of type stdClass can be used in PHP 5 as an alternative to an associative array. An associative array has a number of named properties that can be set and read and so does an object. To start you off, simply cast an associative array into an object:
$info = array("question" => ...
|Objects in PHP - Revision
Part 2 of my "PHP revision" for delegates who will be doing substantial project / systems work. (Part 1 [here]).
Object Oriented techniques give programming languages a practical expandability and robsutness as applications written in them grow in size. Code is separated into classes, with each class ...
|Caching Design Patterns
We run a hotel, and sometimes we have a quandry when it comes to room changeover.
Scenario: John Smith checks out of room 4 at 8 a.m. on Thursday, and looking at our booking system, we see that the room's not been booked out again until Saturday evening, as a twin room, for Deborah Williams ...
|Copying, duplicating, cloning an object in PHP
If you want to copy an object, how do you do it?
Typically, you can just assign the object to a new variable - in PHP, for example:
$demo = new thing("Stick of Celery");
$showme = $demo;
but you need to be careful, because that assigns a second name to the same object. ...
|Autoload in PHP
You can load one file of source from within another in PHP using require or include ... and if you do so directly, you need to manage your calls to them, as if you load a class or function twice, PHP will object. The alternatives require_once and inlcude_once are good, but if you keep calling them with ...
|Design Patterns - what are they? Why use them?
Object orientation provides a wonderful bag of components, but they need to be linked together to provide a full set of facilities for data handling. And very often that linking together is in a way which can be standardised - along the lines of "you normally do it this way". Such standardised methodologies ...
|Designing your application - using UML techniques
When you're planning to write an application, you shouldn't just start writing code - you should think about what you're going to do and plan ahead. But how do you plan ahead? How do you ensure that you've thought of things from various angles?
UML (Unified Modeling Language) is a way of drawing (modeling) ...
|How do classes relate to each other? Associated Classes
How do classes relate to each other? Associated Classes ... or ... define: Associated classes, parent classes, superclasses, subclasses, base classes, abstract classes, interfaces and mixins.
Associated classes are the odd ones out here. They're classes that are used within other classes.
Let me ...
|Planning a hotel refurb - an example of a Gant chart in PHP
We're planning for a grand opening in the Autumn of Well House Manor, and we've a very great deal to do ahead of time. A sound building, 100 years old, is showing the signs of age in a frumpy decor, an access that's suitable for the horse and cart era, and a garden where the Leylandia towers above ...
|Computer Graphics in PHP - World (incoming data) to Pixel (screen) conversion
If you want to draw a diagram or chart on your web page, using data held on your server, chances are that the co-ordinates of the data won't match the pixel numbers that you're using on the screen. A case in point - on this week's PHP Techniques Workshop was a file of data for the years 2005 to ...
|Catchable fatal error in PHP ... How to catch, and alternative solutions such as JSON
When you ask your program to "print out an object", you're really looking to convert a data structure into a stream of characters, and it's not really obvious what you're looking to do.
If you try to print out an object in PHP:
print ($result . "\n");
where the $result variable contains ...
|Private and Public - and things between
A public train service is one which is available for anyone to travel on; a private one only takes limited passengers as invites / made available by the operator. And there could be intermediate levels too - I understand that at times the British Transport Police protect the last train from Weymouth ...
|Does copying a variable duplicate the contents?
If I write a regular assignment statement such as:
$postbag = $mailinglist;
postbag = mailinglist
(depending on the programming language), what am I doing?
"Copying a variable, you plonker!" I hear you saying ... and indeed you're right, but what's really happening? Sometimes, ...
|Getting the OO design write - with PHP a example
One of the great beauties of Object Oriented Design is that you can say "I want to handle a type of data that's based on an xxx", and if someone's already written (and made available) the code for an xxx, all you have do do is define the additions and differences, rather than starting from scratch. ...
|North, Norther and Northest - PHP 5 Objects
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->getlong();
$norther = $ilike->northof($ilike);
|PHP - Object Oriented Design in use
A web server access log file contains a whole series of hit records ... each of which is logically turned into an object as different fields have different characteristics. These hit objects can then be logically arranged into visit objects.
Today, I gave a Object Oriented Programming in PHP training ...
|What is a factory?
A Factory is somewhere that takes raw material, and converts into objects of some value. And so it is in Object Oriented Programming, where a "factory method" takes a chunk of raw material and makes an object out of it. It may differ from a constructor in that we may not know what type of object ...
|The Multiple Inheritance Conundrum, interfaces and mixins
Should an OO programming language support "multiple inheritance"? Let's define multiple inheritance first - starting from simple (single?) inheritance.
I don't want to have to define each type of thing ("class of object") from scratch, so I'll define once class as being based ...
|Static class members in PHP - a documented example
Within a class, you can have dynamic or object variables and methods, where the code and the values associated with it are applied to individual objects. They're far and away the majority of your variables / methods - in yesterday's public transport service exercise, each train or bus had a potentially ...
|Shipping a test harness with your class in PHP
If you're writing a class (or group of classes) into a file you include into your main application(s) in PHP, you'll be recommended to keep the included files out of the document path so that they can't be run on their own (or, more dangerous, browsed as text if you have a different extension applied ...
|Object Oriented Programming in PHP
New ... for the first time ever ... our Object Oriented PHP Programming training course is running on a Monday. That's Monday, 26th April 2010. It then revert to Friday - its more normal day of the week - in May.
PHP's roots were not object oriented ... and in the early days of it being used for ...
|Serialization - storing and reloading objects
In most of the languages we teach, data is held in memory on a "heap" with a "symbol table" holding the names of the variables, where they are stored, and what type of information the (currently) contain. When you write simple variables out to a file (or the screen) functions like print or puts (Tcl) ...
|Abstract classes, Interfaces, PHP and Java
Abstract classes, Interfaces, PHP and Java
When you design code for a number of different similar type of thing (object types, classes) you don't start from scratch in each case - you define a base class which contains all the common code, and you then create subclasses by extending the original. ...
|Object Oriented programming - a practical design example
Object Oriented coding makes enormous sense when you're looking at a medium sized or larger application - but it's a complete change of thought process for the traditional structured programmer.
We ran an Object Oriented Programming in PHP course today, and the course concluded with me taking some sample ...
|Cleaning up redundant objects
In Object Oriented Design, you'll find that there's a piece of code you can add to your classes called the destructor method ... which you don't usually call explicitly in your code.
When a variable goes "out of scope" and is no longer available to your code , it is marked for destruction so that any ...
|PHP4 v PHP5 - Object Model Difference
In PHP, there was a major change in the object model between version 4 and version 5. This code here - incredibly - runs differently on the two versions and what happens when you assign an object to another variable has changed.
In PHP 4, the object being assigned is duplicated, so that if you change ...
|PHP - getclass v instanceof
It should be very rare that you want to say "what type of object is this?" in PHP - or in any OO language; that's because with a correctly implemented class structure, you should have polymorphic effects that cause your code to perform the correct operation whatever object type you run in on. However ...
|Introduction to Object Oriented Programming
"Object Oriented Programming" is a whole new philosophy for programmers who have been writing short scripts for years, and it can be quite frightening to learn with all these new buzzwords like "overriding", "encapsulation" and "polymorphism" creeping in. It's a beautiful concept and a lovely approach, ...
|Sorting objects in PHP
The OO model in PHP is very powerful indeed - and much more code is now written using the facilities it offers than was the case a year or two ago. On yesterday's Object Oriented Programming in PHP course, I wrote a new example that nicely brought together many of the facilities on offer - you can see ...
|Calling base class constructors
In all object oriented languages, you have a facility called inheritance where you can define one type of thing ("class of object") based on another, and the newly defined class ("subclass" or "extended class") takes the initial ("base") class as it starting point.
In your code for your base class, ...
|Accounts in PHP - an OO demo
I've written a new demonstration in PHP ...
* a base class of line items for your accounts
* subclasses for bills and invoices you issue
* a sample application that takes a number of bill and invoice items and produces an accounts table and total
All in the ONE course file as a demo ... here.
|PHP v Java
Question to me yesterday: "Do you have a chart that compares the strengths and weaknesses of the various languages that you teach ...." to which I had to answer "Sorry, no". That's partly because it's like trying to compare apples and oranges, and partly because there are so many different scenarios ...
|Object Oriented Programming in PHP
PHP started life as a language that was used for embedding simple executable content in a web page, and has grown rapidly into a fully fledged system - by release 4 (PHP4) it had become almost as flexible as as any other language for web based applications while retaining its ability to do simple web ...
|OO PHP demonstration - comparing objects and more
Object Oriented PHP course today, and I though I would share an example I wrote during the course with you. Source code here and see it running here.
What does it show?
* A base class matching ... which is extended to a mactainer class, which is further extended to two further subclasses cube and ...
|What are factory and singleton classes?
Do you find some of the OO terminolgy baffling? Once you've learnt about constructors and methods, inheritance, overloading and polymorphism and statics, you might think you're there. Then someone mentions a "factory class" or a "singleton" ...
Fear not - factory and singleton classes are posh names ...
|Object Oriented Model - a summary of changes from PHP4 to PHP5
What are the differences between the Object Oriented model in PHP5 and PHP4?
Firstly (and the reason the release number jumped) is that assignments copy references in PHP5 (which is regarded as correct OO behaviour) but they duplicate or clone the object in PHP4. This may sound a bit obtuse, but:
|Cue the music, I'm happy.
I'm not musical ... but this song keeps jigging around in my head.
"I'm back in the saddle again.
Out where a friend is a friend,
where the longhorn cattle feed
on the lowly gypsum weed.
Back in the saddle again."
First course of the New Year ... done, run and I love it!
Lining up the systems to ...
|Build on what you already have with OO
If I start recalling a "Goons" sketch, then I'll show my age .... but never mind. It was a RERUN that I saw ;-)
Spike Milligan is counting votes at an election. "Two thousand, six hundred and forty two". "Two thousand, six hundred and forty three". "Two thousand, six hundred and forty four" ...
|PHP5 lets you say no
The decision to release "PHP5" rather than a "PHP4.4" was taken due to a specific change in the object model - in PHP4 the code
$second = $first;
would clone an object (duplicate all its contents, whereas in the new model just the reference will be copied, so ...
| Autoload example
| Complete example with polymorphism and inheritance
| Complete OO example
| An Abstract class - cannot construct directly
| Abstract class, extended to a subclass, instantised via the subclass
| Abstract class and interface example
| oo demo step 2 for BBC
| Object copying - beware - differs in PHP4 and PHP5
| object assign and object clone
| Complete inheritance and object method demonstration
| cwa class
| Test program for cwa class
| PHP Version tester
| Simple class in PHP
| Serialization of an object for save and restore
| Simple class in PHP
| Gant chart in PHP
| equip.inc - define pieces of equipment (base class)
| PHP example - objects, foreach, array_push ....
| Explicits PHP4 - objects, foreach, array_push ....
| Example of a factory method and comparators
| Simple class in PHP
| Application to use class hound
| cloning and copying objects
| Namespaces in PHP5 - a short syntax demo
| OO PHP demo - inhertitance, statics, test harness, etc
| Loading in a class from another file and using it
| Two classes, inheritance, factory, JSON, error handler, etc
| PHP application using places.inc - create array of place objects from file
| Test page for the dog.inc class
| A PHP abstract class (places) with postcode, county and other subclasses
| Associative Objects
| Data for Gant Chart Demo
| First OO / class example
| class example - array of objects
| class example - data from file
| class example - factory method for input data
| subclass, polymorphism
| magic __tostring and comparator methods
| Server system - a derived class
| Singleton AND dymanic data
| Data for factory.php demo
| Staff system - a derived class
| Standard Class stdClass
| Static class members - set up and use
| Student system - a derived class
| Objects and inheritance - main worked example program
| Objects and inheritance - main worked example program - PHP4 example
| Data file for systems example
| Command line PHP classes - inheritance, overloading, polymorphism, comparator
| class to be loaded in autoload example
| Inheritance, Overloading, Polymorphism in PHP objects
| Factory method, static method, associated objects
Design and structure.
The limitations of structured programming and the need for objects.
An Introduction to objects in PHP.
Defining simple objects in PHP.
Using an object.
A practical example.
A table of trainee workstations.
A table of all our workstations.
Where do classes load from?.
A comment on objects in PHP.
Objects in PHP5.
Private, protected and public members and the final keyword.
Constructor and destructor methods.
Extensions to Object Inheritance.
PHP’s inheritance model, and Interfaces.
Object model differences PHP4 to PHP5.
Advanced objects in PHP5.
Cloning and equality.
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule
Well House Consultants specialise in training courses in
. We run
throughout the UK (and beyond for longer courses), and
at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if you live in a different
country or continent to us
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre.