corndog_au on Flickr

Many programming languages organize their code into objects, to model their applications and make their code more reusable.

This Code Snippets section of the magazine explains parts of programming languages common across most or all languages. It's a great way to understand how languages work. Learning how to code should be much easier and less frightening if I explain a part of a programming language, then show you how it works in a couple different languages.

Let's get started by talking about how objects are used in Python and PHP.

Object oriented programming is a way to organize software programs. It's a collection of data and functions that do things. It's a way to design applications carefully with an eye to reuse of code. Many languages support objects and object oriented programming.

A class is a template used to create an object. Each object has properties and methods. For example, if you have an object called Dog, you might have properties called a tail or legs. Methods might be eat, drink, walk, sit, and beg. Methods are actions your object can perform. Methods also make sense: it’s unlikely the Dog object would have a method called drive because dogs don’t drive, despite the photo attached above this article.

Since furry pets are fun, we'll talk at a high level about objects used in programming by talking about dogs.

Usually objects are referred to in dot syntax, the use of dots (or periods) between different words that, together, tell the software what to do. Here's an example:


Used as code, this might get your dog to wag its tail. dog is the object, tail is a property. wag is a method or action one or more properties in the dog object can perform. If this pretend bit of code worked, calling dog.tail.wag would cause the dog to wag its tail.

With this as a high level, and non-serious, introduction to the basics of objects and object oriented programming, let's look at how two languages create objects, properties, and methods.


Here's a simple class in Python we can use to create a Dog object:

class Dog:
   " " " Class to represent a dog
   " " "
   def __init__(self)
      self.name = ''
      self.age = 0

At the Python prompt in a command line software tool like Terminal, you would type these lines:

>>>d = Dog()

In this case, typing d.age returns the initial value we defined in line 7 above when we coded a Python class to create the Dog object. Same result for d.name, on line 6 above, which is blank because we did not define a default name for our dog.

Notice the first step is to assign all the details about the Dog object to the variable d: d = Dog(). Then we use the d variable as a prefix at the start of the code to call a property inside our Dog object. This may look like magic but it’s standard in coding to assign one thing to another. You get used to it.

We also can assign values to our name and age properties, again at the Python prompt in our command line software:

>>>d.age = 5

There's a lot more we could do with our Dog object but this provides a high level overview how to define, set, and retrieve properties and methods from a Python object.


PHP uses objects and object-oriented programming in a similar way to Python but with the typical PHP syntax of curly braces { } to mark off the start and end of functions and statements:

class Dog
    //declare properties
    public $name = "Bowser";
    public $age = 0;

    //declare methods
    public function getName() {
        echo $this->name;
    public function printName($string) {
        echo "Your dog&#39;s name: " . $string;

This PHP code will display the name Bowser when called in a PHP script:


While this code will display the name Daisy, or whatever name you put in quotes:


Notice how, unlike Python, we create a variable called $Dog and use it to assign the properties and methods of the Dog object class. The syntax is $Dog-> where the -> indicates we’re assigning everything in the Dog object to this variable then, to the right of the -> characters, we specify what to do and what to method to call.

Also notice on lines 9 and 12 above in our PHP code, we define the getName() and printName() functions as public. Methods in PHP can be public, private, or protected. These are all ways to define the scope, specifically, who can see and use these methods. Private methods, for example, are available only within the object. Perhaps one function in the Dog object needs to call another.

The scope of methods is another aspect of objects common across many programming languages. These examples only demonstrate the basics of how objects, properties, and methods are created in Python and PHP, as well as simple ways to retrieve and update values of properties.

Learn More

Object Oriented Programming


Objects in Python


Objects in PHP


Pros and Cons of Object Oriented Programming (OOP)



  • Tim Slavin

    Tim is an award-winning writer and technologist who enjoys teaching tech to non-technical people. He has many years experience with web sites and applications in business, technical, and creative roles. He and his wife have two kids, now teenagers, who are mad about video games.

Also In The April 2014 Issue

How to Make a Twitter Bot

There are a ton of ways to make a Twitter bot. Here are a number of resources, ideas, and clues to follow up.

What is an Algorithm?

Algorithms control our lives online and offline. Here are some examples to show what is an algorithm and how they work.

How to Build Example Bot

I have updated the installation instructions for this Github project to make it even easier to install and play with.

Bot Makers

A collection of bot makers and professor types attend an online bot convention to talk about Twitter bots and more.

File Extensions

You can tell a bit about the software used to create the web page you're reading by looking at the URL in your web browser.


A computer command used to diagnose network problems also lets you see how data hops across the internet.

How to Build a Slightly Complicated Website

Some tips on how to find online publishing services and use them to create your website.

What is an API?

APIs can be a fun, easy to use, and easy to learn way to retrieve and mash up data online.

April 2014 News Wire

Interesting stories about computer science, software programming, and technology for the month of March 2014.


Many programming languages organize their code into objects, to model their applications and make their code more reusable.

April 2014 Learn More Links

Links from the bottom of all the April 2014 articles, collected in one place for you to print, share, or bookmark.

At bottom, robotics is about us. It is the discipline of emulating our lives, of wondering how we work.

Interested but not ready to subscribe? Sign-up for our free monthly email newsletter with curated site content and a new issue email announcement that we send every two months.

No, thanks!