dark mode light mode Search Menu


corndog_au on Flickr

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)