Web Programming TutorialsLearn About Objects in PHP

Learn About Objects in PHP

Why-Objects

Many new programmers are frightened by the idea of “Object Oriented Programming”. After all, it is a pretty different way of thinking. However, with a little effort you can quickly begin to see the benefits. This article is meant to give you a quick overview of working with classes and objects in PHP. It is by no means exhaustive, and after working through the below examples there will still be much more to learn.

Why Objects?

Modeling with objects is a natural step when you really get down to it. In fact, you’re probably already use to thinking in terms of object. For example, a person could be an object. When looking at a person, it might have properties like its name and age, and might have actions it can perform like talk or walk. So looking at it, it makes complete sense to model real world items like this.

It’s also great for code reuse. As you’ll see a bit later, you’ll only need to write a class once and then have each object of that class inherit the properties of that. For example, in a person class we could have any number of people based off it, each with different names, ages, and any other properties we assign to it. In this way, you’ll see it saves a lot of time to use objects over writing each one out individually.

Important Terms

Before we jump into the code it’s important to understand a couple of key terms. I’ll be using these quite frequently through the rest of this article, so make sure you understand the differences in each one.

  • Class- A class is like a template or blueprint for an object. It defines the various properties that the object should have, and the different actions it can perform.
  • Object- An object is an instance of a class. For example, you might have a person class, with the “Michael” object being one instance of that class. Each object is unique, but shares the elements of their parent class.
  • Member Variables- These are variables defined inside of the object. Each object can have multiple unique variables that describe its traits. For example, the Person class example might have a variable name, which could equal anything from Michael to Sarah.
  • Member Function- Member functions, often referred to as methods, are just like regular functions only they are defined in and call on their object.

Your First Class

Now that we have the general knowledge out of the way, let’s jump right into our first object.

class person{
   public $name = "Michael";
}

$michael = new person();

As you can see, we defined a Person class, and gave it a name attribute. Then, we initialized an object of that class name $michael. It’s really that easy to create an object and class, but let’s see how we can access that data.

class person{
   public $name = "Michael";
}

$michael = new person();

Great! We can now see how to access the data in our object. With the “->” you can access an object’s properties. You’ll also notice the word “public”, don’t worry about that for now, we’ll take a look at that in second.

This is great, but we probably don’t always want to have every object with the same name. Not everyone is named Michael after all!

class person{
   public $name = "";

}


$michael = new person();
$sarah = new person();

$michael->name = "Michael";
$sarah->name = "Sarah";
echo($michael->name);
echo($sarah->name);

Now, we can see that each object has their own unique name. That’s all well and good, but we really can’t do too much with them yet. Let’s teach our people how to speak!

class person{
   public $name = "";

   public function sayName(){
     echo($this->name);
   }
}

$michael = new person();
$sarah = new person();

$michael->name = "Michael";
$sarah->name = "Sarah";
$michael->sayName();
$sarah->sayName();

Now we’re getting somewhere, finally we have some functionality. You’ll also notice the use of “$this” in the “sayName” function. “$this” simply refers to the current object, this is so we can make the function generic enough to work regardless of which object calls it.

By now I hope you can see some of the power of using objects. We could initiate any number of objects we wanted using only one class declaration. As your objects get more complex, you’ll find that it saves a great deal of time not having to rewrite everything. On the topic of complexity why don’t we fill out the class a little more.

Learn PHP Fundamentals From Scratch

Public Private Protected

class person{
   public $name = "Michael";
   private $age = 25;

   public function getAge(){
     echo($this->age);
   }
}

$michael = new Person();

Whoa, there’s some new terms there, let’s take a step back and define what each one means:

  • Access Modifier- This is the public/private/protected in front of the variables and methods in the class. These define the accessibility of the property they precede. Don’t worry if this doesn’t make sense yet, we’ll be going through an example of each one.
  • Public- These properties can be accessed by anyone, even by those outside the class.
  • Private-These properties can only be accessed by members of the class of which they were defined.
  • Protected-These properties can only be accessed by the class of which they were defined and those that inherit it. We won’t be looking at inheritance in this article, so we won’t deal with the protected modifier.
class person{
   public $name = "Michael";
   private $age = 25;

   public function getAge(){
     echo($this->age);
   }
}

$michael = new Person();
echo($michael->name);

As you can see above, there’s no difference, the additional code doesn’t change anything that we’ve learned earlier.

Now, let’s take a look at working with private.

class person{
   public $name = "Michael";
   private $age = 25;

   public function getAge(){
     echo($this->age);
   }
}

$michael = new Person();
echo($michael->getAge);

Whoops, looks like we got an error! It will likely look something like this:

Fatal error: Cannot access private property person::$age
If we take a look back at the definition, it’s clear why we’re trying to access a private variable from outside the class. Let’s rework that a bit, and now we’ll see if it works.

class person{
   public $name = "Michael";
   private $age = 25;

   public function getAge(){
     echo($this->age);
   }
}

$michael = new Person();
$michael->getAge();

That’s better! Now that we are trying to access the data from within the class, there are no issues. This might seem slightly counterintuitive at first, but private variables are very important. They allow us as developers to write code that run the inner workings of our code without exposing it to the outside. Think of it like a business; while a business provides a service to a customer it’s unnecessary for the customer to actually understand the inner workings to still benefit from it.

There you have it, a basic primer on objects! With this you should be able to start working with and integrating classes into your own programs. Just remember, this was only a basic intro. There is still quite a bit to learn, and as you go you’ll learn more and more about objects and how they can help you with your projects.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -