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||updated|
|4073||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 ...
|4057||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" => ...
|3953||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 ...
|3843||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 ...
|3841||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. ...
|3840||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 ...
|3608||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 ...
|3607||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) ...
|3609||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 ...
|720||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 ...
|3211||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 ...
|3210||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 ...
|3142||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 ...
|2921||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, ...
|2922||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. ...
|485||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);
|2774||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 ...
|2741||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 ...
|2717||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 ...
|2680||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 ...
|2632||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 ...
|2641||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 ...
|2435||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) ...
|2434||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. ...
|2380||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 ...
|2171||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 ...
|2172||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 ...
|2169||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 ...
|2160||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 ...
|1925||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, ...
|1820||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 ...
|1819||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, ...
|1682||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.
|124||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 ...
|67||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 ...
|1535||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 ...
|1217||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 ...
|1153||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:
|1027||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 ...
|836||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" ...
|656||Think about your design even if you don't use full UML|
Even if you don't feel that your project is big enough to get involved with formal design methods, many of the lessons of UML and some informal design diagrams can help you get a clear view of what you're going to be doing be for you start, and can help you come up a good, thought out and reliable plan ...
|aa.php|| Autoload example|
|accos.php|| Complete example with polymorphism and inheritance|
|allone.php|| Complete OO example|
|animal1.php|| An Abstract class - cannot construct directly|
|animal2.php|| Abstract class, extended to a subclass, instantised via the subclass|
|animal3.php|| Abstract class and interface example|
|bbcoo2.php|| oo demo step 2 for BBC|
|bewarethedog.php|| Object copying - beware - differs in PHP4 and PHP5|
|clx.php|| object assign and object clone|
|compo.php|| Complete inheritance and object method demonstration|
|cwa.inc|| cwa class|
|cwuseanimal.php|| Test program for cwa class|
|demo.php|| PHP Version tester|
|dog.inc|| Simple class in PHP|
|dogdisc.php|| Serialization of an object for save and restore|
|dogs.inc|| Simple class in PHP|
|elligant.php|| Gant chart in PHP|
|equip.inc|| equip.inc - define pieces of equipment (base class)|
|equtest.php|| PHP example - objects, foreach, array_push ....|
|equtest.php4|| Explicits PHP4 - objects, foreach, array_push ....|
|factory.php|| Example of a factory method and comparators|
|hound.inc|| Simple class in PHP|
|kennel.php|| Application to use class hound|
|litter.php|| cloning and copying objects|
|ns.php|| Namespaces in PHP5 - a short syntax demo|
|ob1.php|| OO PHP demo - inhertitance, statics, test harness, etc|
|ob2.php|| Loading in a class from another file and using it|
|oop5.php|| Two classes, inheritance, factory, JSON, error handler, etc|
|pdemo.php|| PHP application using places.inc - create array of place objects from file|
|pets.php|| Test page for the dog.inc class|
|places.inc|| A PHP abstract class (places) with postcode, county and other subclasses|
|pps|| Data for Gant Chart Demo|
|rest001.php|| First OO / class example|
|rest002.php|| class example - array of objects|
|rest003.php|| class example - data from file|
|rest004.php|| class example - factory method for input data|
|rest005.php|| subclass, polymorphism|
|rest006.php|| magic __tostring and comparator methods|
|serversys.inc|| Server system - a derived class|
|southbound.txt|| Data for factory.php demo|
|staffsys.inc|| Staff system - a derived class|
|stan.php|| Standard Class stdClass|
|staticmember.php|| Static class members - set up and use|
|studentsys.inc|| Student system - a derived class|
|systems.php|| Objects and inheritance - main worked example program|
|systems.php4|| Objects and inheritance - main worked example program - PHP4 example|
|systems.txt|| Data file for systems example|
|third.php|| Command line PHP classes - inheritance, overloading, polymorphism, comparator|
|twigs.inc|| class to be loaded in autoload example|
|valentines.php|| Inheritance, Overloading, Polymorphism in PHP objects|
|visits.php|| 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. Also
available is the Opentalk
Forum for discussion of technical questions.