Objects - The Secret to Learning Object Oriented PHP


If you're looking for an easy way to understand Object Oriented PHP, you've come to the right place. I'm gonna show you the basics. Once you grasp this, you'll be able to learn more advanced concepts.

Here are three things to keep in mind:

  • The most important thing is for you to understand objects. That's the focus of this article.
  • Practice as you go! The easiest way to learn OOP is by doing it.
  • If you have questions or suggestions, get in touch!

So... why all the fuss about objects?

I'm glad you are asked!

Without objects it's very hard to learn modern PHP frameworks, such as Laravel. You won't be able to handle many coding tasks either. The worst part is, you won't be respected as a "serious" developer.

On the other hand, objects make it so much easier to code complex projects. You'll be able to grasp Laravel and other frameworks. It will also be easier to create your own framework as well. More importantly, you'll be able to handle a wider range of jobs and projects.

The question is...

What is the easiest way to understand objects?

Is there a way for me to teach you the basics in 30 minutes or less? Challenge accepted!

Part #1

The best way to understand objects is to create them. That way, your brain will absorb the lesson. When you're done, you'll be able to create PHP objects without thinking.


Let's say you're a Magic Ninja. You have the power to create things... or objects. You can create any object ( or thing ) out of thin air. And the first object you need is to create a fighting sword. You will need to design it on paper, just like any other self-respecting Magic Ninja.

Forget about coding for a moment...

For now, let's just use pen and paper. We want to create something in the "real-world" first. When your brain "sees" it, it can grasp it much better. Then, it will be easier to turn it into code.

Get a piece of paper and a pen right now!

Ready? Let's continue...

Every sword has properties such as:

Most importantly, a good ninja sword also has a

A property is simply a word that can be used to describe an object. For example, flavor is a property of pizza.

Here is my design for a sword:

Next, we will translate this to PHP.

To recap:

My sword can be described like this:

I have designed it on paper and it looks great. (if I do say so myself!) Now, I'm going to translate all this into PHP.

Part #2

( if you missed the first part of the tutorial, click here to read it now.)

To create an object in PHP, you use what we call a class. The way to do it is, you use the word class, then then you name the object you've created. Finally, you will place curly brackets to enclose the contents of your object.

So, to make my sword, I will use PHP code like this:

    class Sword {

Aha! That's already beginning to look great. Wait a minute though... what about my sword's properties? Let's add them now:

    class Sword {

        public $material = 'steel';
        public $color = 'black';
        public $nickname = 'Sexy Monkey';


Nice and crisp. So what do we next? We need some way to use this sword.

In PHP speak, when you want to use an object you need to first instantiate it.

First, you need to create a new variable. This variable will store your object and it's properties for you. Here is how it will look in code:

    $my_sword = new Sword; //That's it!

$my_sword is now an instance of the Sword object. You can access the properties of $my_sword.

For example, if you run this code:

    echo $my_sword->nickname;

You will get:

Sexy Monkey

How cool is that?

Let me quickly explain what just happened here. In order to use a property of an object, you:

Here is another example. This time we want to see what material our sword is made of.

    echo $sword->material;

We should then get:


Here is an exercise for you. Write the code that shows you the color of your sword.

OK, you've done pretty well! Before we continue, here is what you've got to do.

Go back and read through again to make sure you've understood it all. Make sure you do the exercise. Send me and email if you have any questions!

Part #3

Here is what you've learnt in the last section:

Wow! That's a lot of ground covered. Give yourself a tap on the back!

As way of practice, we're going to create another sword. A practice sword. Here is where the power of OOP comes in. We don't have to create the sword from scratch!

We will use the Sword object to create our new practice sword. We will give it the following properties:

Here is how this will look in PHP:

    $practice_sword = new Sword; //We are creating a new instance of the Sword object.

    $practice_sword->material = 'wood'; //Set the material
    $practice_sword->color = 'brown'; //Set the color
    $practice_sword->nickname = 'Fancy Toy';//And of course, we give our sword a nickname.


Once you have an object, it's pretty easy to change it's properties. But, what's the point of being a Magic Ninja if your sword doesn't do anything?

In the next section, we will look at how to add functions to your objects.

Part #4

So we have our Sword - which is pretty cool. The trouble is, we want it to do something. Let's come back to our pen and paper design. We'll write down what we want the sword to do.


My sword will:

In PHP, when you use a function in an object ( or a class ) you call that function a method. I'm not sure why. All you need to remember is that a method is just a function being used as part of an object.

OK. Let's add our first method. We will call that method fight.


    class Sword {

        public $material = 'steel';
        public $color = 'black';
        public $nickname = 'Sexy Monkey';


        public function fight( $bad_guy ){
            echo 'Your wish is my command! I am fighing ' . $bad_guy . '!!! @#%$£!*';


Let's put this in action!

        Create an instance of Sword
        $my_sword = new Sword;

    //Time to fight the Green Troll!

    $my_sword->fight( 'Green Troll' );


When you run this code, you should get:

Your wish is my command. I am fighting Green Troll!!! @#%$£!*

And there you have it! These are the basics of understanding objects in PHP. Of course, there is more to learn. Once you've mastered what I've shared here, it's time to take things to the next level.

You need to learn a few other concepts. On top of that, you will also need to learn how to use OOP to create real life applications.

To help you become a true Magic Ninja that has full control of OOP powers I am creating a course for you. It will cover all you need to know in order to have full control over your applications.

The course will start on 5th September 2017. You will be able to learn at your own pace. You will also get a chance to talk with me live, during Live Classes. So the course will include:

Developers who are not part of us will have to pay \$297 to take part. Yeah, I know that's dirt cheap. But, it gets better. I can't help it, I have to make you an offer you can't refuse.

[Godfather pic]

You can pre-order it a special insider price of \$97. Here is what to do next.

Click on this link - it will take you to the PayPal. Complete the pre-order.

You will then be added to a special mailing list. You'll get early access to some of the course material before everyone else. Pre-order now!