Simple Object Oriented Exercises for Understanding the Basics

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

Do you have other ideas for examples or exercises? Thanks!

+4
source share
5 answers

My blog post about objects in PHP can be helpful:

http://agiletoolkit.org/blog/how-to-use-object-oriented-programming-in-php/

It provides an example of how to grind the geometry classes, triangle, vector, square, etc.

When I studied OOP, I read a book with similar examples in C ++, but I forgot the name.

+2
source

A simple exercise that I have always enjoyed in order to get into an object-oriented way of thinking is to accept some simple concept of the real world and model it into objects. These concepts can be any:

  • Coffee maker
  • A hen
  • Bike
  • and etc.

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

You can effectively create your abstract types according to your external observable characteristics and behavior, their interactions, etc., and then implement these abstractions in PHP or in any other selected language. But the point is to distinguish object-oriented thinking from a concrete implementation.

+1
source

Some things you can use / practical:

  • Almost everything seems to start with a database abstraction library. Very easy to do
  • Abstraction for different caching libraries (memcache, apc, file cache)
  • Perhaps a simple router
0
source

Try the table generation class, where they get the array, and depending on its contents, the table is displayed.

0
source

It depends on how much they already know, if they have experience in C ++ / C programming and start OO, then examples from databases, forms, vectors, etc. will work, if they have relatively little experience, then you need to start with abstract or real life examples, examples are cited in another answer

A coffee maker A chicken A bicycle etc. 

will work great (even though you mentioned that you don't like such examples) in understanding the concept of an object-oriented approach and the theory behind it. They should see how everything works together to form a machine, how each component (class) works and interacts (public methods and interfaces), hiding their own functions and providing a set of services for other components (classes).

Very popular examples that teachers give are radio, where the user does not know what is inside the radio and how it works, the user only knows what he is doing, and the radio makes its functions available to the user by using the buttons on the panel.

These basic examples work well for beginners, and then a programming task should immediately be associated with it. Once they understand the basics, in my opinion, you should use programming and application examples instead of abstract examples.

0
source

All Articles