Class 9 – Basic Concepts of Object Oriented Programming

Posted: April 11th, 2009 | Author: | Filed under: php | Tags: | No Comments »

In class today, we covered the basic concepts of object oriented coding in PHP. Many languages besides PHP allow you to write object-oriented code, and many people find it to be a more intuitive way of writing code than procedural code, which more or less what we have been doing up until now.

The core concept in object oriented programming is the relationship between a class and an object.

Classes

In object oriented programming, a class is an abstract concept of a thing.  A Dog class, for example, would represent the concept “dog”, but not any particular instantiation of that concept.

All Dogs have certain features in common.  They have a name, age, color, pedigree, etc.  There are also actions that all dogs do, such as eat, sleep, bark, chew, pee.

In object oriented programming, we would make a Dog class and in that class we would specify a set of properties that all dogs have, and set of methods, which are the things all dogs can “do”.

In our example, our Dog class would have the following properties:

  • name
  • age
  • color
  • pedigree

And the following methods:

  • eat
  • sleep
  • bark
  • chew
  • pee

Objects

Just as classes are abstract concepts of things, objects are concrete instantiations of those classes.  So, if in our code, we would create objects out of classes.

If you wanted to create a specific instance of a Dog, say a dog called “Sparky”, you would create a Dog object out of the Dog class.  That Dog object would then have all the properties and methods that are defined in the Dog class.

In code, you could then specify what the attributes of this specific instance of a Dog are.  You could set the “name” property of the Dog object to be “Sparky”, set its “age” to be 10, for example.  And you could, in code, tell the Sparky object to bark, chew, eat, or any other method that an instance of the Dog class is capable of doing.

You could create as many Dog objects as you wanted besides Sparky.  They would all be created out of the same Dog class.  The Dog class is like a mold out of which specific Dog objects are made.

Features of Object Oriented Design

There is plenty of documentation of object oriented design concepts online.  Object oriented design offers specific features such as inheritance, abstraction, encapsulation, and polymorphism, which each offer specific advantages, and are intended to be used in specific ways.

Inheritance means that one class may inherit the properties and methods of another class.  For example, a Australian Sheep Dog class and a Shiba Inu class may both inherit the same basic properties and methods described in a Dog class.  We would say they were sub-classes that inherit from a Dog class.  But they may also add more specific properties and methods that only apply to objects of their specific sub-class.

Abstraction means that how the class implements the various properties and methods is not important to usage of the class.  For example, I could tell either an Australian Sheep Dog or a Cat to go to sleep, without worrying about how each of those classes implemented that behavior.

Encapsulation means that the specific methods and attributes that apply to one object may have the same name as the methods and attributes of another object, but they are specific to each object.  So the methods and properties of Dogs are encapsulated within the Dog class, and are thereby separated from any methods and properties that apply to Cats, for example.  Furthermore, the age property of Sparky is encapsulated with the Sparky object, while the age property of ChooChoo is encapsulated within the ChooChoo object, and is totally unrelated to Sparky’s age.  Encapsulated data may be totally invisible to the outside world.

Polymorphism is the concept that many different kinds of objects (of different classes) may respond to the same sorts of instructions.  For example, we can tell a Cat to scratch itself, and we can tell a Monkey to scratch itself.  But how they do the scratching will be totally different.  A Monkey will use its hands, while a Cat will use its back feet.  The point is that many Classes may share a common set of commands to which they respond, but how they implement those commands may be totally different.

Related posts:

  1. Class 9 – Anatomy of Object-Oriented Programming in PHP


Leave a Reply