PHP classes and objects

→ Are you a new visitor? Please visit the page guidance for new visitors ←

PHP classes and objects

Few days ago I had a talk with a few friends of mine about this topic, and I saw that some people are still misunderstanding the OOP paradigm in programming. So I want to clarify a few things in this article.

What is OOP ?

OOP stands for Object Oriented Programming and it represents concepts as objects that have attributes that interact with them, and associated procedures known as methods.

What are classes and objects?

A class is a collection of methods and properties, pretty simple declarations. You can also say that a class is a “definition” for an object. A class can contain properties (they can be variables or constants) and functions that are called methods.

An object is an instance of a class that is created to give you power to set, get or use methods and properties of the class.

Did you get the difference? No? Let’s see an example:

In the above example, our class is declared with the “class” keyword, and the given name “myClass”. Now, if you look at it, you will see that our class is constructed from one property called “$message” and one function called “saySomething”.

This is the class, now, let’s create an object for this class:

An object is created with the “new” operator and the name of the class that we want to create an object from. After the object is created we can access properties and methods from the class with the “->”  declaration and the name of the property or method from inside it. The above example creates an object of “myClass” , calls the “saySomething” method. This method calls internaly the $message property, gets its value and return it, so the content of $message property will be stored in the $result variable and outputed later on with “echo”.

Still not clear? Yes, I know, we used “$this” and you are thinking “hey, what is that?”. If used in a class, “$this” refers to the object that it’s in. So saying “$this->message” we access the $message variable from within our class.


You saw that our method “saySomething” is declared with the keyword “public”. So what is that? A property or a method can be one of the following three things: public, private or protected.

PUBLIC - scope to make that variable / function available from anywhere, other classes and instances of the object.

PRIVATE - scope when you want your variable / function to be visible in its own class only.

PROTECTED - scope when you want to make your variable / function visible in all classes that extend current class including the parent class.

So as an example, if we want our method “saySomething” to be used only inside class, we would declare it private not public as it is right now, so when we instantiate the class, we cannot access that method.

The constructor of the class

The who? well, this is new huh? A constructor is a function that is called when the class is instantiated and the object is created.

Let’s see an example:

So we added a special function that is declared in front of it’s name with two underscores “__”. When we instantiate the class and create the object for that class, the constructor is called with the given parameters, and sets a new value to the $message property.

See? now the message it is not the default one from the $message property, but a new one that we gave it in the constructor.

In php4 you don’t have the keyword __constructor() so if you want to use one you can declare a function inside the class with the same name as the class itself and it works as a constructor.

HINT!  By declaring the constructor of the class private or protected, we cannot instantiate that class anymore. This is used for the singleton pattern but this is another topic that I will cover in a later article.

Why OOP ?

Finally, we want to see what is the entire purpose of this OOP thing right ?

  • It improves code re-usability. You haven’t changed any of the old code. You haven’t introduced any new bugs in old code.
  • OOP can both make development faster, and your programs run faster.
  • Scalable code. If you write procedural code in a big project, you will see yourself with a more buggy and less understood project for designers.
  • Encapsulation. You can create modular classes that can be reused in other applications with little to no modification.

So I guess I clarified a few things with this article and you all learned something good (if not new).

Until next time, happy coding! :)

Request an article ←

You may also like-