Sign up ×

I'm teaching a few friends some basic object-oriented concepts in PHP and i wanted to give them some real-world examples, but simple, so they can grasp the syntax and the basics of OO. I already gave them the following exercise: create a small class that abstracts HTML form creation. You can create objects for each form field, each type of field has a class. I also have a form class which receives form field objects.

Do you guys have any other ideas of examples or exercises? Thanks!

share|improve this question

closed as not constructive by svick, Kirk Woll, Paŭlo Ebermann, James Montagne, Graviton Oct 5 '11 at 3:52


5 Answers 5

My blog post about Objects in PHP might be useful:

It gives example how to macerate "geometry" classes, triangle, vector, square etc.

When I was learning OOP, I was reading a book with similar examples in C++, but I forgot the name.

share|improve this answer
Your link is dead, can you fix it? – j08691 Jul 13 at 20:18

A simple exercise I've always enjoyed for getting into an object-oriented way of thinking is to take some simple real-world concept and model it into objects. These concepts can be anything:

  • A coffee maker
  • A chicken
  • A bicycle
  • etc.

It's very language-agnostic, platform-agnostic, etc. The idea is to abstract out all of the implementation details (things like PHP and HTML) and focus on object-oriented thinking. How does a model for such an object look? What are its attributes? What are its constraints? How does it behave? How does it interact with other objects?

You can effectively design your abstract types by their external observable characteristics and behaviors, their interactions, etc. and then implement those abstractions in PHP or any other language of choice. But the point is to separate out the object-oriented thinking from the specific implementation.

share|improve this answer
I don't really like abstract examples because they don't actually do anything. It's hard to explain the concept of OO without some hands-on exercises, application-oriented. – Smithson Oct 3 '11 at 19:44
The best way to explain the concepts of OO is to abstract it. OO is a language agnostic paradigm. Otherwise, you run the risk of the student getting trapped into a language-specific implementation. Teach the concept, then work on implementation. – Herbert Oct 12 '11 at 23:55

Some things that are usable/practical:

  • Almost everybody seems to start with a database abstraction library. Very simple to do
  • Abstraction for different caching libraries (memcache, apc, file cache)
  • Perhaps a simple router
share|improve this answer

Try a table generation class, where they receive an array and, depending on it's contents, a table is displayed.

share|improve this answer

It depends on how much they know already, if they have experience of programming in C++/C and are starting OO, then examples from Databases, forms, vector etc would work, if they have relatively little experience, then you have to start with abstract or real life examples, examples quoted in another reply

A coffee maker
A chicken
A bicycle

will work great (despite you have mentioned you do not like such examples) in understanding the concept of Object Oriented and the theory behind it. They have to see how everything works together to form a machine, how each component (class) works and interacts (public methods and interfaces) while hiding its own functionality, and providing a set of services to other components (classes).

A very popular examples teachers give is of a radio, where the user doesn't know what is inside the radio and how it works, the user only knows what it does, and the radio makes its features available to the user for use through the buttons on the panel.

These basic examples work well for beginners, and then immediately there should be a programming assignment related to it. Once they understand the basics, programming and application examples instead of abstract examples should be used in my opinion.

share|improve this answer
