Develo Design


Introduction to Object Oriented PHP – Pt. 1

A year of working as a Front-end Magento developer without major PHP experience has been a steep learning curve and despite having learned a great deal, I thought it was about time I got some serious PHP knowledge under my belt.

My Plan: To nail the fundamentals of Object-Oriented PHP.

The End Goal: To get a thorough understanding of what is going on under the hood of PHP frameworks, to help me write better code and debug issues with minimal perspiration.

Whats the point of Object Oriented Programming?

Working with Objects allows us to wrap functionality and properties for a specific entity together. This keeps things separate and allows us to access those properties and functions without fear of anything spilling over on to any innocent bystanders.

To the untrained eye, OOP can seem like an unnecessary effort but it makes code more maintainable and easier to modify, and when applications grow, it can and will save a lot of headaches.

The majority of PHP frameworks will implement an object-oriented approach, so if you want to gain an understanding of whats going on behind the scenes, it is important to gain a thorough understanding of object oriented PHP.

Need to Know?

There are a few OOP-specific terms which you will need to know:

  • Class: What a thing should look like.
  • Object: A thing
  • Instantiate: To create a new thing. (A new object with a given class).
  • Method: A function belonging to a class.
  • Property: A variable belonging to a class.


Animal Class

First thing we will do is create a new class. For the sake of this example I will create an ‘Animal’ class.

What we have done here is:

  • Defined a new Animal Class.
  • Said that aAnimal should have a ‘name’ property.
  • Created the method that will run when a new Animal is created.    __construct() is a standard method for all Classes and it explains how to build a new instantiation of that Class. In this case, it takes the value passed in and set it as the new Animals name.

Now that our Animal blueprint exists, lets make a new Animal.

This will create a new Animal with the name Rufus which we can access through the $dog reference. e.g. $dog->name.

At the moment Animals are pretty limited. Lets teach them to say hello:

What is “$this”?! I hear you scream.

To explain the use of $this, let me extend the movesBy method in the above example slightly. Turns out, every Wednesday Rufus becomes a Super Dog and is able to fly.

This example is a bit weird, but it helps demonstrate the point so stick with me. When working with objects, it is important to distinguish properties and methods of an object ( e.g. an animals name and how it moves around) from variables used in the construction of those properties and methods (such as the $dayofweek and $today variables defined above). As you can see in the above example, PHP offers $this as a non-contextual reference to the current object.

The above example is fairly abstract but its helpful in getting a grasp on these concepts before moving on. Coming soon: Extending a Class, Access Modifiers.


Loading Disqus Comments ...
Loading Facebook Comments ...

Post Comment

Your email address will not be published. Required fields are marked *

Discuss My Project

We want to hear about your project! Provide us with a few details and we’ll get back to you for a chat.